コード例 #1
0
ファイル: KMSWebApp.cs プロジェクト: orf53975/hadoop.net
 private void InitLogging(string confDir)
 {
     if (Runtime.GetProperty("log4j.configuration") == null)
     {
         Runtime.SetProperty("log4j.defaultInitOverride", "true");
         bool     fromClasspath = true;
         FilePath log4jConf     = new FilePath(confDir, Log4jProperties).GetAbsoluteFile();
         if (log4jConf.Exists())
         {
             PropertyConfigurator.ConfigureAndWatch(log4jConf.GetPath(), 1000);
             fromClasspath = false;
         }
         else
         {
             ClassLoader cl       = Thread.CurrentThread().GetContextClassLoader();
             Uri         log4jUrl = cl.GetResource(Log4jProperties);
             if (log4jUrl != null)
             {
                 PropertyConfigurator.Configure(log4jUrl);
             }
         }
         Log = LoggerFactory.GetLogger(typeof(KMSWebApp));
         Log.Debug("KMS log starting");
         if (fromClasspath)
         {
             Log.Warn("Log4j configuration file '{}' not found", Log4jProperties);
             Log.Warn("Logging with INFO level to standard output");
         }
     }
     else
     {
         Log = LoggerFactory.GetLogger(typeof(KMSWebApp));
     }
 }
コード例 #2
0
        /// <summary>Create an HttpFS Server to talk to the MiniDFSCluster we created.</summary>
        /// <exception cref="System.Exception"/>
        private void CreateHttpFSServer()
        {
            FilePath homeDir = TestDirHelper.GetTestDir();

            NUnit.Framework.Assert.IsTrue(new FilePath(homeDir, "conf").Mkdir());
            NUnit.Framework.Assert.IsTrue(new FilePath(homeDir, "log").Mkdir());
            NUnit.Framework.Assert.IsTrue(new FilePath(homeDir, "temp").Mkdir());
            HttpFSServerWebApp.SetHomeDirForCurrentThread(homeDir.GetAbsolutePath());
            FilePath   secretFile = new FilePath(new FilePath(homeDir, "conf"), "secret");
            TextWriter w          = new FileWriter(secretFile);

            w.Write("secret");
            w.Close();
            // HDFS configuration
            FilePath hadoopConfDir = new FilePath(new FilePath(homeDir, "conf"), "hadoop-conf"
                                                  );

            if (!hadoopConfDir.Mkdirs())
            {
                throw new IOException();
            }
            string        fsDefaultName = nnConf.Get(CommonConfigurationKeysPublic.FsDefaultNameKey);
            Configuration conf          = new Configuration(false);

            conf.Set(CommonConfigurationKeysPublic.FsDefaultNameKey, fsDefaultName);
            // Explicitly turn off XAttr support
            conf.SetBoolean(DFSConfigKeys.DfsNamenodeXattrsEnabledKey, false);
            FilePath     hdfsSite = new FilePath(hadoopConfDir, "hdfs-site.xml");
            OutputStream os       = new FileOutputStream(hdfsSite);

            conf.WriteXml(os);
            os.Close();
            // HTTPFS configuration
            conf = new Configuration(false);
            conf.Set("httpfs.hadoop.config.dir", hadoopConfDir.ToString());
            conf.Set("httpfs.proxyuser." + HadoopUsersConfTestHelper.GetHadoopProxyUser() + ".groups"
                     , HadoopUsersConfTestHelper.GetHadoopProxyUserGroups());
            conf.Set("httpfs.proxyuser." + HadoopUsersConfTestHelper.GetHadoopProxyUser() + ".hosts"
                     , HadoopUsersConfTestHelper.GetHadoopProxyUserHosts());
            conf.Set("httpfs.authentication.signature.secret.file", secretFile.GetAbsolutePath
                         ());
            FilePath httpfsSite = new FilePath(new FilePath(homeDir, "conf"), "httpfs-site.xml"
                                               );

            os = new FileOutputStream(httpfsSite);
            conf.WriteXml(os);
            os.Close();
            ClassLoader cl  = Sharpen.Thread.CurrentThread().GetContextClassLoader();
            Uri         url = cl.GetResource("webapp");

            if (url == null)
            {
                throw new IOException();
            }
            WebAppContext context = new WebAppContext(url.AbsolutePath, "/webhdfs");

            Org.Mortbay.Jetty.Server server = TestJettyHelper.GetJettyServer();
            server.AddHandler(context);
            server.Start();
        }
コード例 #3
0
        public override Uri GetResource(string name)
        {
            Uri url = null;

            if (!IsSystemClass(name, systemClasses))
            {
                url = FindResource(name);
                if (url == null && name.StartsWith("/"))
                {
                    if (Log.IsDebugEnabled())
                    {
                        Log.Debug("Remove leading / off " + name);
                    }
                    url = FindResource(Runtime.Substring(name, 1));
                }
            }
            if (url == null)
            {
                url = parent.GetResource(name);
            }
            if (url != null)
            {
                if (Log.IsDebugEnabled())
                {
                    Log.Debug("getResource(" + name + ")=" + url);
                }
            }
            return(url);
        }
コード例 #4
0
            /// <exception cref="System.IO.IOException"/>
            public virtual void Setup <_T0>(TaskInputOutputContext <_T0> context)
            {
                Configuration conf = context.GetConfiguration();

                Path[]     localFiles    = context.GetLocalCacheFiles();
                URI[]      files         = context.GetCacheFiles();
                Path[]     localArchives = context.GetLocalCacheArchives();
                URI[]      archives      = context.GetCacheArchives();
                FileSystem fs            = LocalFileSystem.Get(conf);

                // Check that 2 files and 2 archives are present
                NUnit.Framework.Assert.AreEqual(2, localFiles.Length);
                NUnit.Framework.Assert.AreEqual(2, localArchives.Length);
                NUnit.Framework.Assert.AreEqual(2, files.Length);
                NUnit.Framework.Assert.AreEqual(2, archives.Length);
                // Check the file name
                NUnit.Framework.Assert.IsTrue(files[0].GetPath().EndsWith("distributed.first"));
                NUnit.Framework.Assert.IsTrue(files[1].GetPath().EndsWith("distributed.second.jar"
                                                                          ));
                // Check lengths of the files
                NUnit.Framework.Assert.AreEqual(1, fs.GetFileStatus(localFiles[0]).GetLen());
                NUnit.Framework.Assert.IsTrue(fs.GetFileStatus(localFiles[1]).GetLen() > 1);
                // Check extraction of the archive
                NUnit.Framework.Assert.IsTrue(fs.Exists(new Path(localArchives[0], "distributed.jar.inside3"
                                                                 )));
                NUnit.Framework.Assert.IsTrue(fs.Exists(new Path(localArchives[1], "distributed.jar.inside4"
                                                                 )));
                // Check the class loaders
                Log.Info("Java Classpath: " + Runtime.GetProperty("java.class.path"));
                ClassLoader cl = Sharpen.Thread.CurrentThread().GetContextClassLoader();

                // Both the file and the archive were added to classpath, so both
                // should be reachable via the class loader.
                NUnit.Framework.Assert.IsNotNull(cl.GetResource("distributed.jar.inside2"));
                NUnit.Framework.Assert.IsNotNull(cl.GetResource("distributed.jar.inside3"));
                NUnit.Framework.Assert.IsNull(cl.GetResource("distributed.jar.inside4"));
                // Check that the symlink for the renaming was created in the cwd;
                NUnit.Framework.Assert.IsTrue("symlink distributed.first.symlink doesn't exist",
                                              symlinkFile.Exists());
                NUnit.Framework.Assert.AreEqual("symlink distributed.first.symlink length not 1",
                                                1, symlinkFile.Length());
                //This last one is a difference between MRv2 and MRv1
                NUnit.Framework.Assert.IsTrue("second file should be symlinked too", expectedAbsentSymlinkFile
                                              .Exists());
            }
コード例 #5
0
        public static Uri GetResource(string name)
        {
            Uri         url         = null;
            ClassLoader classLoader = CustomClassLoader;

            if (classLoader != null)
            {
                url = classLoader.GetResource(name);
            }
            if (url == null)
            {
                // Try the current Thread context classloader
                //classLoader = Thread.CurrentThread.ContextClassLoader;
                url = classLoader.GetResource(name);
            }

            return(url);
        }
コード例 #6
0
        /// <exception cref="System.Exception"/>
        private void CreateHttpFSServer()
        {
            FilePath homeDir = TestDirHelper.GetTestDir();

            NUnit.Framework.Assert.IsTrue(new FilePath(homeDir, "conf").Mkdir());
            NUnit.Framework.Assert.IsTrue(new FilePath(homeDir, "log").Mkdir());
            NUnit.Framework.Assert.IsTrue(new FilePath(homeDir, "temp").Mkdir());
            HttpFSServerWebApp.SetHomeDirForCurrentThread(homeDir.GetAbsolutePath());
            FilePath   secretFile = new FilePath(new FilePath(homeDir, "conf"), "secret");
            TextWriter w          = new FileWriter(secretFile);

            w.Write("secret");
            w.Close();
            //HDFS configuration
            FilePath hadoopConfDir = new FilePath(new FilePath(homeDir, "conf"), "hadoop-conf"
                                                  );

            hadoopConfDir.Mkdirs();
            string fsDefaultName = TestHdfsHelper.GetHdfsConf().Get(CommonConfigurationKeysPublic
                                                                    .FsDefaultNameKey);
            Configuration conf = new Configuration(false);

            conf.Set(CommonConfigurationKeysPublic.FsDefaultNameKey, fsDefaultName);
            FilePath     hdfsSite = new FilePath(hadoopConfDir, "hdfs-site.xml");
            OutputStream os       = new FileOutputStream(hdfsSite);

            conf.WriteXml(os);
            os.Close();
            conf = new Configuration(false);
            conf.Set("httpfs.proxyuser.client.hosts", "*");
            conf.Set("httpfs.proxyuser.client.groups", "*");
            conf.Set("httpfs.authentication.type", "kerberos");
            conf.Set("httpfs.authentication.signature.secret.file", secretFile.GetAbsolutePath
                         ());
            FilePath httpfsSite = new FilePath(new FilePath(homeDir, "conf"), "httpfs-site.xml"
                                               );

            os = new FileOutputStream(httpfsSite);
            conf.WriteXml(os);
            os.Close();
            ClassLoader   cl      = Sharpen.Thread.CurrentThread().GetContextClassLoader();
            Uri           url     = cl.GetResource("webapp");
            WebAppContext context = new WebAppContext(url.AbsolutePath, "/webhdfs");

            Org.Mortbay.Jetty.Server server = TestJettyHelper.GetJettyServer();
            server.AddHandler(context);
            server.Start();
            HttpFSServerWebApp.Get().SetAuthority(TestJettyHelper.GetAuthority());
        }
コード例 #7
0
        private static string GetTestRuntimeClasspath()
        {
            Log.Info("Trying to generate classpath for app master from current thread's classpath"
                     );
            string envClassPath = string.Empty;
            string cp           = Runtime.GetProperty("java.class.path");

            if (cp != null)
            {
                envClassPath += cp.Trim() + FilePath.pathSeparator;
            }
            // yarn-site.xml at this location contains proper config for mini cluster
            ClassLoader thisClassLoader = Sharpen.Thread.CurrentThread().GetContextClassLoader
                                              ();
            Uri url = thisClassLoader.GetResource("yarn-site.xml");

            envClassPath += new FilePath(url.GetFile()).GetParent();
            return(envClassPath);
        }
コード例 #8
0
        /// <exception cref="System.IO.IOException"/>
        private JavaKeyStoreProvider(URI uri, Configuration conf)
            : base(conf)
        {
            this.uri = uri;
            path     = ProviderUtils.UnnestUri(uri);
            fs       = path.GetFileSystem(conf);
            // Get the password file from the conf, if not present from the user's
            // environment var
            if (Runtime.GetEnv().Contains(KeystorePasswordEnvVar))
            {
                password = Runtime.Getenv(KeystorePasswordEnvVar).ToCharArray();
            }
            if (password == null)
            {
                string pwFile = conf.Get(KeystorePasswordFileKey);
                if (pwFile != null)
                {
                    ClassLoader cl      = Thread.CurrentThread().GetContextClassLoader();
                    Uri         pwdFile = cl.GetResource(pwFile);
                    if (pwdFile == null)
                    {
                        // Provided Password file does not exist
                        throw new IOException("Password file does not exists");
                    }
                    using (InputStream @is = pwdFile.OpenStream())
                    {
                        password = IOUtils.ToString(@is).Trim().ToCharArray();
                    }
                }
            }
            if (password == null)
            {
                password = KeystorePasswordDefault;
            }
            try
            {
                Path oldPath = ConstructOldPath(path);
                Path newPath = ConstructNewPath(path);
                keyStore = KeyStore.GetInstance(SchemeName);
                FsPermission perm = null;
                if (fs.Exists(path))
                {
                    // flush did not proceed to completion
                    // _NEW should not exist
                    if (fs.Exists(newPath))
                    {
                        throw new IOException(string.Format("Keystore not loaded due to some inconsistency "
                                                            + "('%s' and '%s' should not exist together)!!", path, newPath));
                    }
                    perm = TryLoadFromPath(path, oldPath);
                }
                else
                {
                    perm = TryLoadIncompleteFlush(oldPath, newPath);
                }
                // Need to save off permissions in case we need to
                // rewrite the keystore in flush()
                permissions = perm;
            }
            catch (KeyStoreException e)
            {
                throw new IOException("Can't create keystore", e);
            }
            catch (NoSuchAlgorithmException e)
            {
                throw new IOException("Can't load keystore " + path, e);
            }
            catch (CertificateException e)
            {
                throw new IOException("Can't load keystore " + path, e);
            }
            ReadWriteLock Lock = new ReentrantReadWriteLock(true);

            readLock  = Lock.ReadLock();
            writeLock = Lock.WriteLock();
        }
コード例 #9
0
        /// <exception cref="System.Exception"/>
        private void CreateHttpFSServer(bool addDelegationTokenAuthHandler)
        {
            FilePath homeDir = TestDirHelper.GetTestDir();

            NUnit.Framework.Assert.IsTrue(new FilePath(homeDir, "conf").Mkdir());
            NUnit.Framework.Assert.IsTrue(new FilePath(homeDir, "log").Mkdir());
            NUnit.Framework.Assert.IsTrue(new FilePath(homeDir, "temp").Mkdir());
            HttpFSServerWebApp.SetHomeDirForCurrentThread(homeDir.GetAbsolutePath());
            FilePath   secretFile = new FilePath(new FilePath(homeDir, "conf"), "secret");
            TextWriter w          = new FileWriter(secretFile);

            w.Write("secret");
            w.Close();
            //HDFS configuration
            FilePath hadoopConfDir = new FilePath(new FilePath(homeDir, "conf"), "hadoop-conf"
                                                  );

            hadoopConfDir.Mkdirs();
            string fsDefaultName = TestHdfsHelper.GetHdfsConf().Get(CommonConfigurationKeysPublic
                                                                    .FsDefaultNameKey);
            Configuration conf = new Configuration(false);

            conf.Set(CommonConfigurationKeysPublic.FsDefaultNameKey, fsDefaultName);
            conf.SetBoolean(DFSConfigKeys.DfsNamenodeAclsEnabledKey, true);
            conf.SetBoolean(DFSConfigKeys.DfsNamenodeXattrsEnabledKey, true);
            FilePath     hdfsSite = new FilePath(hadoopConfDir, "hdfs-site.xml");
            OutputStream os       = new FileOutputStream(hdfsSite);

            conf.WriteXml(os);
            os.Close();
            //HTTPFS configuration
            conf = new Configuration(false);
            if (addDelegationTokenAuthHandler)
            {
                conf.Set("httpfs.authentication.type", typeof(HttpFSKerberosAuthenticationHandlerForTesting
                                                              ).FullName);
            }
            conf.Set("httpfs.services.ext", typeof(TestHttpFSServer.MockGroups).FullName);
            conf.Set("httpfs.admin.group", HadoopUsersConfTestHelper.GetHadoopUserGroups(HadoopUsersConfTestHelper
                                                                                         .GetHadoopUsers()[0])[0]);
            conf.Set("httpfs.proxyuser." + HadoopUsersConfTestHelper.GetHadoopProxyUser() + ".groups"
                     , HadoopUsersConfTestHelper.GetHadoopProxyUserGroups());
            conf.Set("httpfs.proxyuser." + HadoopUsersConfTestHelper.GetHadoopProxyUser() + ".hosts"
                     , HadoopUsersConfTestHelper.GetHadoopProxyUserHosts());
            conf.Set("httpfs.authentication.signature.secret.file", secretFile.GetAbsolutePath
                         ());
            conf.Set("httpfs.hadoop.config.dir", hadoopConfDir.ToString());
            FilePath httpfsSite = new FilePath(new FilePath(homeDir, "conf"), "httpfs-site.xml"
                                               );

            os = new FileOutputStream(httpfsSite);
            conf.WriteXml(os);
            os.Close();
            ClassLoader   cl      = Sharpen.Thread.CurrentThread().GetContextClassLoader();
            Uri           url     = cl.GetResource("webapp");
            WebAppContext context = new WebAppContext(url.AbsolutePath, "/webhdfs");

            Org.Mortbay.Jetty.Server server = TestJettyHelper.GetJettyServer();
            server.AddHandler(context);
            server.Start();
            if (addDelegationTokenAuthHandler)
            {
                HttpFSServerWebApp.Get().SetAuthority(TestJettyHelper.GetAuthority());
            }
        }
コード例 #10
0
            /// <exception cref="System.IO.IOException"/>
            protected override void Setup(Mapper.Context context)
            {
                Configuration conf = context.GetConfiguration();

                Path[] localFiles    = context.GetLocalCacheFiles();
                URI[]  files         = context.GetCacheFiles();
                Path[] localArchives = context.GetLocalCacheArchives();
                URI[]  archives      = context.GetCacheArchives();
                // Check that 4 (2 + appjar + DistrubutedCacheChecker jar) files
                // and 2 archives are present
                NUnit.Framework.Assert.AreEqual(4, localFiles.Length);
                NUnit.Framework.Assert.AreEqual(4, files.Length);
                NUnit.Framework.Assert.AreEqual(2, localArchives.Length);
                NUnit.Framework.Assert.AreEqual(2, archives.Length);
                // Check lengths of the files
                IDictionary <string, Path> filesMap = PathsToMap(localFiles);

                NUnit.Framework.Assert.IsTrue(filesMap.Contains("distributed.first.symlink"));
                NUnit.Framework.Assert.AreEqual(1, localFs.GetFileStatus(filesMap["distributed.first.symlink"
                                                                         ]).GetLen());
                NUnit.Framework.Assert.IsTrue(filesMap.Contains("distributed.second.jar"));
                NUnit.Framework.Assert.IsTrue(localFs.GetFileStatus(filesMap["distributed.second.jar"
                                                                    ]).GetLen() > 1);
                // Check extraction of the archive
                IDictionary <string, Path> archivesMap = PathsToMap(localArchives);

                NUnit.Framework.Assert.IsTrue(archivesMap.Contains("distributed.third.jar"));
                NUnit.Framework.Assert.IsTrue(localFs.Exists(new Path(archivesMap["distributed.third.jar"
                                                                      ], "distributed.jar.inside3")));
                NUnit.Framework.Assert.IsTrue(archivesMap.Contains("distributed.fourth.jar"));
                NUnit.Framework.Assert.IsTrue(localFs.Exists(new Path(archivesMap["distributed.fourth.jar"
                                                                      ], "distributed.jar.inside4")));
                // Check the class loaders
                Log.Info("Java Classpath: " + Runtime.GetProperty("java.class.path"));
                ClassLoader cl = Sharpen.Thread.CurrentThread().GetContextClassLoader();

                // Both the file and the archive should have been added to classpath, so
                // both should be reachable via the class loader.
                NUnit.Framework.Assert.IsNotNull(cl.GetResource("distributed.jar.inside2"));
                NUnit.Framework.Assert.IsNotNull(cl.GetResource("distributed.jar.inside3"));
                NUnit.Framework.Assert.IsNotNull(cl.GetResource("distributed.jar.inside4"));
                // The Job Jar should have been extracted to a folder named "job.jar" and
                // added to the classpath; the two jar files in the lib folder in the Job
                // Jar should have also been added to the classpath
                NUnit.Framework.Assert.IsNotNull(cl.GetResource("job.jar/"));
                NUnit.Framework.Assert.IsNotNull(cl.GetResource("job.jar/lib/lib1.jar"));
                NUnit.Framework.Assert.IsNotNull(cl.GetResource("job.jar/lib/lib2.jar"));
                // Check that the symlink for the renaming was created in the cwd;
                FilePath symlinkFile = new FilePath("distributed.first.symlink");

                NUnit.Framework.Assert.IsTrue(symlinkFile.Exists());
                NUnit.Framework.Assert.AreEqual(1, symlinkFile.Length());
                // Check that the symlink for the Job Jar was created in the cwd and
                // points to the extracted directory
                FilePath jobJarDir = new FilePath("job.jar");

                if (Shell.Windows)
                {
                    NUnit.Framework.Assert.IsTrue(IsWindowsSymlinkedDirectory(jobJarDir));
                }
                else
                {
                    NUnit.Framework.Assert.IsTrue(FileUtils.IsSymlink(jobJarDir));
                    NUnit.Framework.Assert.IsTrue(jobJarDir.IsDirectory());
                }
            }
コード例 #11
0
        /// <exception cref="System.IO.IOException"/>
        protected internal AbstractJavaKeyStoreProvider(URI uri, Configuration conf)
        {
            this.uri = uri;
            InitFileSystem(uri, conf);
            // Get the password from the user's environment
            if (Runtime.GetEnv().Contains(CredentialPasswordName))
            {
                password = Runtime.Getenv(CredentialPasswordName).ToCharArray();
            }
            // if not in ENV get check for file
            if (password == null)
            {
                string pwFile = conf.Get(KeystorePasswordFileKey);
                if (pwFile != null)
                {
                    ClassLoader cl      = Thread.CurrentThread().GetContextClassLoader();
                    Uri         pwdFile = cl.GetResource(pwFile);
                    if (pwdFile != null)
                    {
                        using (InputStream @is = pwdFile.OpenStream())
                        {
                            password = IOUtils.ToString(@is).Trim().ToCharArray();
                        }
                    }
                }
            }
            if (password == null)
            {
                password = KeystorePasswordDefault.ToCharArray();
            }
            try
            {
                keyStore = KeyStore.GetInstance("jceks");
                if (KeystoreExists())
                {
                    StashOriginalFilePermissions();
                    using (InputStream @in = GetInputStreamForFile())
                    {
                        keyStore.Load(@in, password);
                    }
                }
                else
                {
                    CreatePermissions("700");
                    // required to create an empty keystore. *sigh*
                    keyStore.Load(null, password);
                }
            }
            catch (KeyStoreException e)
            {
                throw new IOException("Can't create keystore", e);
            }
            catch (NoSuchAlgorithmException e)
            {
                throw new IOException("Can't load keystore " + GetPathAsString(), e);
            }
            catch (CertificateException e)
            {
                throw new IOException("Can't load keystore " + GetPathAsString(), e);
            }
            ReadWriteLock Lock = new ReentrantReadWriteLock(true);

            readLock  = Lock.ReadLock();
            writeLock = Lock.WriteLock();
        }
コード例 #12
0
ファイル: Beans.cs プロジェクト: ranganathsb/JavaSharp
        /// <summary>
        /// Instantiate a bean.
        /// <para>
        /// The bean is created based on a name relative to a class-loader.
        /// This name should be a dot-separated name such as "a.b.c".
        /// </para>
        /// <para>
        /// In Beans 1.0 the given name can indicate either a serialized object
        /// or a class.  Other mechanisms may be added in the future.  In
        /// beans 1.0 we first try to treat the beanName as a serialized object
        /// name then as a class name.
        /// </para>
        /// <para>
        /// When using the beanName as a serialized object name we convert the
        /// given beanName to a resource pathname and add a trailing ".ser" suffix.
        /// We then try to load a serialized object from that resource.
        /// </para>
        /// <para>
        /// For example, given a beanName of "x.y", Beans.instantiate would first
        /// try to read a serialized object from the resource "x/y.ser" and if
        /// that failed it would try to load the class "x.y" and create an
        /// instance of that class.
        /// </para>
        /// <para>
        /// If the bean is a subtype of java.applet.Applet, then it is given
        /// some special initialization.  First, it is supplied with a default
        /// AppletStub and AppletContext.  Second, if it was instantiated from
        /// a classname the applet's "init" method is called.  (If the bean was
        /// deserialized this step is skipped.)
        /// </para>
        /// <para>
        /// Note that for beans which are applets, it is the caller's responsiblity
        /// to call "start" on the applet.  For correct behaviour, this should be done
        /// after the applet has been added into a visible AWT container.
        /// </para>
        /// <para>
        /// Note that applets created via beans.instantiate run in a slightly
        /// different environment than applets running inside browsers.  In
        /// particular, bean applets have no access to "parameters", so they may
        /// wish to provide property get/set methods to set parameter values.  We
        /// advise bean-applet developers to test their bean-applets against both
        /// the JDK appletviewer (for a reference browser environment) and the
        /// BDK BeanBox (for a reference bean container).
        ///
        /// </para>
        /// </summary>
        /// <returns> a JavaBean </returns>
        /// <param name="cls">         the class-loader from which we should create
        ///                        the bean.  If this is null, then the system
        ///                        class-loader is used. </param>
        /// <param name="beanName">    the name of the bean within the class-loader.
        ///                        For example "sun.beanbox.foobah" </param>
        /// <param name="beanContext"> The BeanContext in which to nest the new bean </param>
        /// <param name="initializer"> The AppletInitializer for the new bean
        /// </param>
        /// <exception cref="ClassNotFoundException"> if the class of a serialized
        ///              object could not be found. </exception>
        /// <exception cref="IOException"> if an I/O error occurs. </exception>

//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public static Object instantiate(ClassLoader cls, String beanName, java.beans.beancontext.BeanContext beanContext, AppletInitializer initializer) throws java.io.IOException, ClassNotFoundException
        public static Object Instantiate(ClassLoader cls, String beanName, BeanContext beanContext, AppletInitializer initializer)
        {
            InputStream       ins;
            ObjectInputStream oins   = null;
            Object            result = null;
            bool        serialized   = false;
            IOException serex        = null;

            // If the given classloader is null, we check if an
            // system classloader is available and (if so)
            // use that instead.
            // Note that calls on the system class loader will
            // look in the bootstrap class loader first.
            if (cls == null)
            {
                try
                {
                    cls = ClassLoader.SystemClassLoader;
                }
                catch (SecurityException)
                {
                    // We're not allowed to access the system class loader.
                    // Drop through.
                }
            }

            // Try to find a serialized object with this name
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final String serName = beanName.replace('.','/').concat(".ser");
            String serName = beanName.Replace('.', '/') + ".ser";

            if (cls == null)
            {
                ins = ClassLoader.GetSystemResourceAsStream(serName);
            }
            else
            {
                ins = cls.GetResourceAsStream(serName);
            }
            if (ins != null)
            {
                try
                {
                    if (cls == null)
                    {
                        oins = new ObjectInputStream(ins);
                    }
                    else
                    {
                        oins = new ObjectInputStreamWithLoader(ins, cls);
                    }
                    result     = oins.ReadObject();
                    serialized = true;
                    oins.Close();
                }
                catch (IOException ex)
                {
                    ins.Close();
                    // Drop through and try opening the class.  But remember
                    // the exception in case we can't find the class either.
                    serex = ex;
                }
                catch (ClassNotFoundException ex)
                {
                    ins.Close();
                    throw ex;
                }
            }

            if (result == null)
            {
                // No serialized object, try just instantiating the class
                Class cl;

                try
                {
                    cl = ClassFinder.findClass(beanName, cls);
                }
                catch (ClassNotFoundException ex)
                {
                    // There is no appropriate class.  If we earlier tried to
                    // deserialize an object and got an IO exception, throw that,
                    // otherwise rethrow the ClassNotFoundException.
                    if (serex != null)
                    {
                        throw serex;
                    }
                    throw ex;
                }

                if (!Modifier.isPublic(cl.Modifiers))
                {
                    throw new ClassNotFoundException("" + cl + " : no public access");
                }

                /*
                 * Try to instantiate the class.
                 */

                try
                {
                    result = cl.NewInstance();
                }
                catch (Exception ex)
                {
                    // We have to remap the exception to one in our signature.
                    // But we pass extra information in the detail message.
                    throw new ClassNotFoundException("" + cl + " : " + ex, ex);
                }
            }

            if (result != null)
            {
                // Ok, if the result is an applet initialize it.

                AppletStub stub = null;

                if (result is Applet)
                {
                    Applet applet      = (Applet)result;
                    bool   needDummies = initializer == null;

                    if (needDummies)
                    {
                        // Figure our the codebase and docbase URLs.  We do this
                        // by locating the URL for a known resource, and then
                        // massaging the URL.

                        // First find the "resource name" corresponding to the bean
                        // itself.  So a serialzied bean "a.b.c" would imply a
                        // resource name of "a/b/c.ser" and a classname of "x.y"
                        // would imply a resource name of "x/y.class".

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final String resourceName;
                        String resourceName;

                        if (serialized)
                        {
                            // Serialized bean
                            resourceName = beanName.Replace('.', '/') + ".ser";
                        }
                        else
                        {
                            // Regular class
                            resourceName = beanName.Replace('.', '/') + ".class";
                        }

                        URL objectUrl = null;
                        URL codeBase  = null;
                        URL docBase   = null;

                        // Now get the URL correponding to the resource name.
                        if (cls == null)
                        {
                            objectUrl = ClassLoader.GetSystemResource(resourceName);
                        }
                        else
                        {
                            objectUrl = cls.GetResource(resourceName);
                        }

                        // If we found a URL, we try to locate the docbase by taking
                        // of the final path name component, and the code base by taking
                        // of the complete resourceName.
                        // So if we had a resourceName of "a/b/c.class" and we got an
                        // objectURL of "file://bert/classes/a/b/c.class" then we would
                        // want to set the codebase to "file://bert/classes/" and the
                        // docbase to "file://bert/classes/a/b/"

                        if (objectUrl != null)
                        {
                            String s = objectUrl.ToExternalForm();

                            if (s.EndsWith(resourceName))
                            {
                                int ix = s.Length() - resourceName.Length();
                                codeBase = new URL(s.Substring(0, ix));
                                docBase  = codeBase;

                                ix = s.LastIndexOf('/');

                                if (ix >= 0)
                                {
                                    docBase = new URL(s.Substring(0, ix + 1));
                                }
                            }
                        }

                        // Setup a default context and stub.
                        BeansAppletContext context = new BeansAppletContext(applet);

                        stub        = (AppletStub) new BeansAppletStub(applet, context, codeBase, docBase);
                        applet.Stub = stub;
                    }
                    else
                    {
                        initializer.Initialize(applet, beanContext);
                    }

                    // now, if there is a BeanContext, add the bean, if applicable.

                    if (beanContext != null)
                    {
                        UnsafeBeanContextAdd(beanContext, result);
                    }

                    // If it was deserialized then it was already init-ed.
                    // Otherwise we need to initialize it.

                    if (!serialized)
                    {
                        // We need to set a reasonable initial size, as many
                        // applets are unhappy if they are started without
                        // having been explicitly sized.
                        applet.SetSize(100, 100);
                        applet.Init();
                    }

                    if (needDummies)
                    {
                        ((BeansAppletStub)stub).Active_Renamed = true;
                    }
                    else
                    {
                        initializer.Activate(applet);
                    }
                }
                else if (beanContext != null)
                {
                    UnsafeBeanContextAdd(beanContext, result);
                }
            }

            return(result);
        }
コード例 #13
0
        /// <exception cref="System.Exception"/>
        public virtual void Start()
        {
            ClassLoader cl = Thread.CurrentThread().GetContextClassLoader();

            Runtime.SetProperty(KMSConfiguration.KmsConfigDir, kmsConfDir);
            FilePath aclsFile = new FilePath(kmsConfDir, "kms-acls.xml");

            if (!aclsFile.Exists())
            {
                InputStream  @is = cl.GetResourceAsStream("mini-kms-acls-default.xml");
                OutputStream os  = new FileOutputStream(aclsFile);
                IOUtils.Copy(@is, os);
                @is.Close();
                os.Close();
            }
            FilePath coreFile = new FilePath(kmsConfDir, "core-site.xml");

            if (!coreFile.Exists())
            {
                Configuration core   = new Configuration();
                TextWriter    writer = new FileWriter(coreFile);
                core.WriteXml(writer);
                writer.Close();
            }
            FilePath kmsFile = new FilePath(kmsConfDir, "kms-site.xml");

            if (!kmsFile.Exists())
            {
                Configuration kms = new Configuration(false);
                kms.Set(KMSConfiguration.KeyProviderUri, "jceks://file@" + new Path(kmsConfDir, "kms.keystore"
                                                                                    ).ToUri());
                kms.Set("hadoop.kms.authentication.type", "simple");
                TextWriter writer = new FileWriter(kmsFile);
                kms.WriteXml(writer);
                writer.Close();
            }
            Runtime.SetProperty("log4j.configuration", log4jConfFile);
            jetty = CreateJettyServer(keyStore, keyStorePassword, inPort);
            // we need to do a special handling for MiniKMS to work when in a dir and
            // when in a JAR in the classpath thanks to Jetty way of handling of webapps
            // when they are in the a DIR, WAR or JAR.
            Uri webXmlUrl = cl.GetResource("kms-webapp/WEB-INF/web.xml");

            if (webXmlUrl == null)
            {
                throw new RuntimeException("Could not find kms-webapp/ dir in test classpath");
            }
            bool   webXmlInJar = webXmlUrl.AbsolutePath.Contains(".jar!/");
            string webappPath;

            if (webXmlInJar)
            {
                FilePath webInf = new FilePath("target/" + UUID.RandomUUID().ToString() + "/kms-webapp/WEB-INF"
                                               );
                webInf.Mkdirs();
                new FilePath(webInf, "web.xml").Delete();
                InputStream  @is = cl.GetResourceAsStream("kms-webapp/WEB-INF/web.xml");
                OutputStream os  = new FileOutputStream(new FilePath(webInf, "web.xml"));
                IOUtils.Copy(@is, os);
                @is.Close();
                os.Close();
                webappPath = webInf.GetParentFile().GetAbsolutePath();
            }
            else
            {
                webappPath = cl.GetResource("kms-webapp").AbsolutePath;
            }
            WebAppContext context = new WebAppContext(webappPath, "/kms");

            if (webXmlInJar)
            {
                context.SetClassLoader(cl);
            }
            jetty.AddHandler(context);
            jetty.Start();
            kmsURL = new Uri(GetJettyURL(jetty), "kms");
        }