View Javadoc

1   package org.apache.jcs.auxiliary.remote;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.rmi.RMISecurityManager;
25  import java.rmi.RemoteException;
26  import java.rmi.registry.LocateRegistry;
27  import java.rmi.registry.Registry;
28  import java.util.Enumeration;
29  import java.util.Properties;
30  
31  import org.apache.commons.logging.Log;
32  import org.apache.commons.logging.LogFactory;
33  
34  /***
35   * This class provides some basic utilities for doing things such as starting
36   * the registry properly.
37   */
38  public class RemoteUtils
39  {
40      private final static Log log = LogFactory.getLog( RemoteUtils.class );
41  
42      /*** No instances please. */
43      private RemoteUtils()
44      {
45          super();
46      }
47  
48      /***
49       * Creates and exports a registry on the specified port of the local host.
50       * <p>
51       * @param port
52       * @return the port the registry was started on
53       * @throws RemoteException
54       */
55      public static int createRegistry( int port )
56          throws RemoteException
57      {
58          if ( log.isInfoEnabled() )
59          {
60              log.info( "createRegistry> setting security manager" );
61          }
62          System.setSecurityManager( new RMISecurityManager() );
63  
64          if ( port < 1024 )
65          {
66              if ( log.isInfoEnabled() )
67              {
68                  log.info( "Port chosen was less than 1024, will use default [" + Registry.REGISTRY_PORT + "] instead." );
69              }
70              port = Registry.REGISTRY_PORT;
71          }
72  
73          if ( log.isInfoEnabled() )
74          {
75              log.info( "createRegistry> creating registry on port [" + port + "]" );
76          }
77          LocateRegistry.createRegistry( port );
78          return port;
79      }
80  
81      /***
82       * Loads properties for the named props file.
83       * <p>
84       * @param propFile
85       * @return The properties object for the file
86       * @throws IOException
87       */
88      public static Properties loadProps( String propFile )
89          throws IOException
90      {
91          InputStream is = RemoteUtils.class.getResourceAsStream( propFile );
92          Properties props = new Properties();
93          try
94          {
95              props.load( is );
96              if ( log.isDebugEnabled() )
97              {
98                  log.debug( "props.size=" + props.size() );
99              }
100 
101             if ( log.isDebugEnabled() )
102             {
103                 if ( props != null )
104                 {
105                     Enumeration en = props.keys();
106                     StringBuffer buf = new StringBuffer();
107                     while ( en.hasMoreElements() )
108                     {
109                         String key = (String) en.nextElement();
110                         buf.append( "\n" + key + " = " + props.getProperty( key ) );
111                     }
112                     log.debug( buf.toString() );
113                 }
114                 else
115                 {
116                     log.debug( "props is null" );
117                 }
118             }
119 
120         }
121         catch ( Exception ex )
122         {
123             log.error( "Error loading remote properties, for file name [" + propFile + "]", ex );
124         }
125         finally
126         {
127             if ( is != null )
128             {
129                 is.close();
130             }
131         }
132         return props;
133     }
134 }