Return-Path:
- * Configuration looks like:
- *
- * <myBlock>
- * <data-source name="default"
- * class="org.apache.avalon.excalibur.datasource.JdbcDataSource">
- * <!-- configuration for JdbcDataSource -->
- * <pool-controller min="5" max="10" connection-class="my.overrided.ConnectionClass">
- * <keep-alive>select 1</keep-alive>
- * </pool-controller>
- * <driver>com.database.jdbc.JdbcDriver</driver>
- * <dburl>jdbc:driver://host/mydb</dburl>
- * <user>username</user>
- * <password>password</password>
- * </data-source>
- * </myBlock>
- *
- *
- * @phoenix:block
- * @phoenix:service name="org.apache.avalon.cornerstone.services.datasource.DataSourceSelector"
- *
- * @author Eung-ju Park
- */
-public class DefaultDataSourceSelector
- extends AbstractLogEnabled
- implements DataSourceSelector, Contextualizable, Configurable, Initializable, Disposable
-{
- private Configuration m_configuration;
- private Map m_dataSources;
- private BlockContext m_context;
-
- public void contextualize( final Context context )
- throws ContextException
- {
- m_context = (BlockContext)context;
- }
-
- /**
- * @phoenix:configuration-schema type="relax-ng"
- */
- public void configure( final Configuration configuration )
- {
- m_configuration = configuration;
- }
-
- public void initialize()
- throws Exception
- {
- m_dataSources = new HashMap();
-
- Configuration[] dataSourceConfs = getDataSourceConfig();
-
- for( int i = 0; i < dataSourceConfs.length; i++ )
- {
- final Configuration dataSourceConf = dataSourceConfs[ i ];
-
- final String name = dataSourceConf.getAttribute( "name" );
- final String clazz = dataSourceConf.getAttribute( "class" );
- final String driver = dataSourceConf.getChild( "driver", true ).getValue( "" );
-
- final ClassLoader classLoader =
- Thread.currentThread().getContextClassLoader();
-
- DataSourceComponent component = null;
- if( null == classLoader )
- {
- if( !"".equals( driver ) )
- {
- Class.forName( driver, true, Thread.currentThread().getContextClassLoader() );
- }
-
- component = (DataSourceComponent)Class.forName( clazz ).newInstance();
- }
- else
- {
- if( !"".equals( driver ) )
- {
- classLoader.loadClass( driver );
- }
-
- component = (DataSourceComponent)classLoader.loadClass( clazz ).newInstance();
- }
-
- if( component instanceof LogEnabled )
- {
- setupLogger( component, name );
- }
- component.configure( dataSourceConf );
- m_dataSources.put( name, component );
-
- if( getLogger().isInfoEnabled() )
- {
- getLogger().info( "DataSource " + name + " ready" );
- }
- }
- }
-
- private Configuration[] getDataSourceConfig()
- {
- final Configuration head =
- m_configuration.getChild( "data-sources" );
- if( 0 != head.getChildren().length )
- {
- final String message =
- "WARNING: Child node
+ * <myBlock>
+ * <data-source name="default"
+ * class="org.apache.avalon.excalibur.datasource.JdbcDataSource">
+ * <!-- configuration for JdbcDataSource -->
+ * <pool-controller min="5" max="10" connection-class="my.overrided.ConnectionClass">
+ * <keep-alive>select 1</keep-alive>
+ * </pool-controller>
+ * <driver>com.database.jdbc.JdbcDriver</driver>
+ * <dburl>jdbc:driver://host/mydb</dburl>
+ * <user>username</user>
+ * <password>password</password>
+ * </data-source>
+ * </myBlock>
+ *
+ *
+ * @phoenix:block
+ * @phoenix:service name="org.apache.avalon.cornerstone.services.datasource.DataSourceSelector"
+ *
+ * @author Eung-ju Park
+ */
+public class DefaultDataSourceSelector
+ extends AbstractLogEnabled
+ implements DataSourceSelector, Contextualizable, Configurable, Initializable, Disposable
+{
+ private Configuration m_configuration;
+ private Map m_dataSources;
+ private String m_blockName;
+
+ public void contextualize( final Context context )
+ throws ContextException
+ {
+ m_blockName = (String) context.get("block.name");
+ }
+
+ /**
+ * @phoenix:configuration-schema type="relax-ng"
+ */
+ public void configure( final Configuration configuration )
+ {
+ m_configuration = configuration;
+ }
+
+ public void initialize()
+ throws Exception
+ {
+ m_dataSources = new HashMap();
+
+ Configuration[] dataSourceConfs = getDataSourceConfig();
+
+ for( int i = 0; i < dataSourceConfs.length; i++ )
+ {
+ final Configuration dataSourceConf = dataSourceConfs[ i ];
+
+ final String name = dataSourceConf.getAttribute( "name" );
+ final String clazz = dataSourceConf.getAttribute( "class" );
+ final String driver = dataSourceConf.getChild( "driver", true ).getValue( "" );
+
+ final ClassLoader classLoader =
+ Thread.currentThread().getContextClassLoader();
+
+ DataSourceComponent component = null;
+ if( null == classLoader )
+ {
+ if( !"".equals( driver ) )
+ {
+ Class.forName( driver, true, Thread.currentThread().getContextClassLoader() );
+ }
+
+ component = (DataSourceComponent)Class.forName( clazz ).newInstance();
+ }
+ else
+ {
+ if( !"".equals( driver ) )
+ {
+ classLoader.loadClass( driver );
+ }
+
+ component = (DataSourceComponent)classLoader.loadClass( clazz ).newInstance();
+ }
+
+ if( component instanceof LogEnabled )
+ {
+ setupLogger( component, name );
+ }
+ component.configure( dataSourceConf );
+ m_dataSources.put( name, component );
+
+ if( getLogger().isInfoEnabled() )
+ {
+ getLogger().info( "DataSource " + name + " ready" );
+ }
+ }
+ }
+
+ private Configuration[] getDataSourceConfig()
+ {
+ final Configuration head =
+ m_configuration.getChild( "data-sources" );
+ if( 0 != head.getChildren().length )
+ {
+
+ final String message =
+ "WARNING: Child node
- * <ssl-factory>
- * <keystore>
- * <file>conf/keystore</file> <!-- keystore file location -->
- * <password></password> <!-- Key Store file password, only used to check keystore integrity -->
- * <key-password></key-password> <!-- Only required when you need to decrypt a private key -->
- * <type>JKS</type> <!-- Key Store file format, defaults to JKS -->
- * <algorithm>SunX509</algorithm> <!-- Cryptography provider ID, defaults to SunX509 -->
- * </keystore>
- * <!-- SSL protocol to use, defaults to TLS, another possible value is SSL -->
- * <protocol>TLS</protocol>
- * </ssl-factory>
- *
- *
- * Notes on keystore files. Absolute paths are supported. Relative - * paths are interpreted relative to .sar base directory. Defaults to - * conf/keystore. Since keystore usually contains sensitive keys it - * maybe beneficial to not include the keystores into the .sar - * files. - *
- * @author Greg Steuck - */ -public class SSLFactoryBuilder extends AbstractLogEnabled - implements Configurable, Contextualizable, Disposable, Initializable -{ - private File m_baseDirectory; - private File m_keystoreFile; - - private String m_keystorePassword; - private String m_keyPassword; - private String m_protocol; - private String m_provider; - private String m_keystoreFormat; - - private SSLContext m_ctx; - - static - { - // Registers Sun's providers - java.security.Security.addProvider( new sun.security.provider.Sun() ); - java.security.Security.addProvider( new com.sun.net.ssl.internal.ssl.Provider() ); - } - - /** - * Requires a BlockContext. We'll see how we end up expressing - * these dependencies. - */ - public void contextualize( final Context context ) - { - final BlockContext blockContext = (BlockContext) context; - m_baseDirectory = blockContext.getBaseDirectory(); - } - - public void configure( final Configuration configuration ) - throws ConfigurationException - { - final Configuration storeConfig = configuration.getChild( "keystore" ); - final String fileName = storeConfig.getChild( "file" ).getValue( "conf/keystore" ); - final File configuredFile = new File( fileName ); - if ( ! configuredFile.isAbsolute() ) - { - m_keystoreFile = new File ( m_baseDirectory, fileName ); - } - else - { - m_keystoreFile = configuredFile; - } - - m_keystorePassword = storeConfig.getChild( "password" ).getValue( null ); - m_keyPassword = storeConfig.getChild( "key-password" ).getValue( null ); - // key is named incorrectly, left as is for compatibility - m_provider = storeConfig.getChild( "algorithm" ).getValue( "SunX509" ); - // key is named incorrectly, left as is for compatibility - m_keystoreFormat = storeConfig.getChild( "type" ).getValue( "JKS" ); - // ugly compatibility workaround follows - m_protocol = configuration.getChild( "protocol" ). - getValue( storeConfig.getChild( "protocol" ).getValue( "TLS" ) ); - } - - /** - * Produces a fresh ssl socket factory with configured parameters. - */ - public SSLSocketFactory buildSocketFactory() - { - return m_ctx.getSocketFactory(); - } - - /** - * Produces a fresh ssl server socket factory with configured - * parameters. - */ - public SSLServerSocketFactory buildServerSocketFactory() - { - return m_ctx.getServerSocketFactory(); - } - - public void initialize() - throws IOException, GeneralSecurityException - { - final FileInputStream keyStream = new FileInputStream( m_keystoreFile ); - try - { - m_ctx = makeContext( keyStream, m_keystorePassword, - m_keyPassword, m_protocol, - m_provider, m_keystoreFormat ); - } - finally - { - try - { - keyStream.close(); - } - catch ( IOException e ) - { - // avoids hiding exceptions from makeContext - // by catching this IOException - getLogger().error( "Error keyStream.close failed", e ); - } - } - } - - public void dispose() - { - m_keystorePassword = null; - m_keyPassword = null; - } - - /** - * Creates an SSL context which uses the keys and certificates - * provided by the given keyStream. For simplicity the - * same key stream (keystore) is used for both key and trust - * factory. - * - * @param keyStream to read the keys from - * @param keystorePassword password for the keystore, can be null - * if integrity verification is not desired - * @param keyPassword passphrase which unlocks the keys in the key file - * (should really be a char[] so that it can be cleaned after use) - * @param protocol the standard name of the requested protocol - * @param provider the standard name of the requested algorithm - * @param keystoreFormat the type of keystore - * - * @return context configured with these keys and certificates - * @throws IOException if files can't be read - * @throws GeneralSecurityException is something goes wrong inside - * cryptography framework - */ - private static SSLContext makeContext (InputStream keyStream, - String keystorePassword, - String keyPassword, - String protocol, - String provider, - String keystoreFormat ) - throws IOException, GeneralSecurityException - { - final KeyStore keystore = loadKeystore( keyStream, - keystorePassword, - keystoreFormat ); - final KeyManagerFactory kmf = KeyManagerFactory.getInstance( provider ); - // even though undocumented Sun's implementation doesn't allow - // null passphrases, but zero sized arrays are OK - final char [] passChars = ( keyPassword != null ) ? - keyPassword.toCharArray() : new char [0]; - try - { - kmf.init( keystore, passChars ); - } - finally - { - Arrays.fill( passChars, (char) 0 ); - } - - final TrustManagerFactory tmf = - TrustManagerFactory.getInstance( provider ); - tmf.init( keystore ); - - final SSLContext result = SSLContext.getInstance( protocol ); - result.init( kmf.getKeyManagers(), - tmf.getTrustManagers(), - new java.security.SecureRandom() ); - return result; - } - - /** - * Builds a keystore loaded from the given stream. The passphrase - * is used to verify the keystore file integrity. - * @param file to load from - * @param passphrase for the store integrity verification (or null if - * integrity check is not wanted) - * @param keystoreFormat the type of keystore - * @return loaded key store - * @throws IOException if file can not be read - * @throws GeneralSecurityException if key store can't be built - */ - private static KeyStore loadKeystore( InputStream keyStream, - String passphrase, - String keystoreFormat ) - throws GeneralSecurityException, IOException - { - final KeyStore ks = KeyStore.getInstance( keystoreFormat ); - - if ( passphrase != null ) - { - final char [] passChars = passphrase.toCharArray(); - try - { - ks.load( keyStream, passChars ); - } - finally - { - Arrays.fill( passChars, (char) 0 ); - } - } - else - { - ks.load( keyStream, null ); - } - - return ks; - } -} +/* + * Copyright (C) The Apache Software Foundation. All rights reserved. + * + * This software is published under the terms of the Apache Software License + * version 1.1, a copy of which has been included with this distribution in + * the LICENSE.txt file. + */ +package org.apache.avalon.cornerstone.blocks.sockets; + +import com.sun.net.ssl.KeyManagerFactory; +import com.sun.net.ssl.SSLContext; +import com.sun.net.ssl.TrustManagerFactory; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.security.GeneralSecurityException; +import java.security.KeyStore; +import java.util.Arrays; +import javax.net.ssl.SSLServerSocketFactory; +import javax.net.ssl.SSLSocketFactory; +import org.apache.avalon.framework.activity.Disposable; +import org.apache.avalon.framework.activity.Initializable; +import org.apache.avalon.framework.configuration.Configurable; +import org.apache.avalon.framework.configuration.Configuration; +import org.apache.avalon.framework.configuration.ConfigurationException; +import org.apache.avalon.framework.context.Context; +import org.apache.avalon.framework.context.Contextualizable; +import org.apache.avalon.framework.logger.AbstractLogEnabled; +import org.apache.avalon.framework.context.*; + +/** + * Builds SSLContexts with desired properties. Hides all the gory + * details of SSLContext productions behind nice Avalon + * interfaces. Married to Sun JCA implementation. + *+ * Configuration looks like: + *
+ * <ssl-factory> + * <keystore> + * <file>conf/keystore</file> <!-- keystore file location --> + * <password></password> <!-- Key Store file password, only used to check keystore integrity --> + * <key-password></key-password> <!-- Only required when you need to decrypt a private key --> + * <type>JKS</type> <!-- Key Store file format, defaults to JKS --> + * <algorithm>SunX509</algorithm> <!-- Cryptography provider ID, defaults to SunX509 --> + * </keystore> + * <!-- SSL protocol to use, defaults to TLS, another possible value is SSL --> + * <protocol>TLS</protocol> + * </ssl-factory> + *+ * + *
+ * Notes on keystore files. Absolute paths are supported. Relative + * paths are interpreted relative to .sar base directory. Defaults to + * conf/keystore. Since keystore usually contains sensitive keys it + * maybe beneficial to not include the keystores into the .sar + * files. + *
+ * @author Greg Steuck + */ +public class SSLFactoryBuilder extends AbstractLogEnabled + implements Configurable, Contextualizable, Disposable, Initializable +{ + private File m_baseDirectory; + private File m_keystoreFile; + + private String m_keystorePassword; + private String m_keyPassword; + private String m_protocol; + private String m_provider; + private String m_keystoreFormat; + + private SSLContext m_ctx; + + static + { + // Registers Sun's providers + java.security.Security.addProvider( new sun.security.provider.Sun() ); + java.security.Security.addProvider( new com.sun.net.ssl.internal.ssl.Provider() ); + } + + /** + * Requires a BlockContext. We'll see how we end up expressing + * these dependencies. + */ + public void contextualize( final Context context ) throws ContextException + { + m_baseDirectory = (File) context.get("app.home"); + } + + public void configure( final Configuration configuration ) + throws ConfigurationException + { + final Configuration storeConfig = configuration.getChild( "keystore" ); + final String fileName = storeConfig.getChild( "file" ).getValue( "conf/keystore" ); + final File configuredFile = new File( fileName ); + if ( ! configuredFile.isAbsolute() ) + { + m_keystoreFile = new File ( m_baseDirectory, fileName ); + } + else + { + m_keystoreFile = configuredFile; + } + + m_keystorePassword = storeConfig.getChild( "password" ).getValue( null ); + m_keyPassword = storeConfig.getChild( "key-password" ).getValue( null ); + // key is named incorrectly, left as is for compatibility + m_provider = storeConfig.getChild( "algorithm" ).getValue( "SunX509" ); + // key is named incorrectly, left as is for compatibility + m_keystoreFormat = storeConfig.getChild( "type" ).getValue( "JKS" ); + // ugly compatibility workaround follows + m_protocol = configuration.getChild( "protocol" ). + getValue( storeConfig.getChild( "protocol" ).getValue( "TLS" ) ); + } + + /** + * Produces a fresh ssl socket factory with configured parameters. + */ + public SSLSocketFactory buildSocketFactory() + { + return m_ctx.getSocketFactory(); + } + + /** + * Produces a fresh ssl server socket factory with configured + * parameters. + */ + public SSLServerSocketFactory buildServerSocketFactory() + { + return m_ctx.getServerSocketFactory(); + } + + public void initialize() + throws IOException, GeneralSecurityException + { + final FileInputStream keyStream = new FileInputStream( m_keystoreFile ); + try + { + m_ctx = makeContext( keyStream, m_keystorePassword, + m_keyPassword, m_protocol, + m_provider, m_keystoreFormat ); + } + finally + { + try + { + keyStream.close(); + } + catch ( IOException e ) + { + // avoids hiding exceptions from makeContext + // by catching this IOException + getLogger().error( "Error keyStream.close failed", e ); + } + } + } + + public void dispose() + { + m_keystorePassword = null; + m_keyPassword = null; + } + + /** + * Creates an SSL context which uses the keys and certificates + * provided by the given keyStream. For simplicity the + * same key stream (keystore) is used for both key and trust + * factory. + * + * @param keyStream to read the keys from + * @param keystorePassword password for the keystore, can be null + * if integrity verification is not desired + * @param keyPassword passphrase which unlocks the keys in the key file + * (should really be a char[] so that it can be cleaned after use) + * @param protocol the standard name of the requested protocol + * @param provider the standard name of the requested algorithm + * @param keystoreFormat the type of keystore + * + * @return context configured with these keys and certificates + * @throws IOException if files can't be read + * @throws GeneralSecurityException is something goes wrong inside + * cryptography framework + */ + private static SSLContext makeContext (InputStream keyStream, + String keystorePassword, + String keyPassword, + String protocol, + String provider, + String keystoreFormat ) + throws IOException, GeneralSecurityException + { + final KeyStore keystore = loadKeystore( keyStream, + keystorePassword, + keystoreFormat ); + final KeyManagerFactory kmf = KeyManagerFactory.getInstance( provider ); + // even though undocumented Sun's implementation doesn't allow + // null passphrases, but zero sized arrays are OK + final char [] passChars = ( keyPassword != null ) ? + keyPassword.toCharArray() : new char [0]; + try + { + kmf.init( keystore, passChars ); + } + finally + { + Arrays.fill( passChars, (char) 0 ); + } + + final TrustManagerFactory tmf = + TrustManagerFactory.getInstance( provider ); + tmf.init( keystore ); + + final SSLContext result = SSLContext.getInstance( protocol ); + result.init( kmf.getKeyManagers(), + tmf.getTrustManagers(), + new java.security.SecureRandom() ); + return result; + } + + /** + * Builds a keystore loaded from the given stream. The passphrase + * is used to verify the keystore file integrity. + * @param file to load from + * @param passphrase for the store integrity verification (or null if + * integrity check is not wanted) + * @param keystoreFormat the type of keystore + * @return loaded key store + * @throws IOException if file can not be read + * @throws GeneralSecurityException if key store can't be built + */ + private static KeyStore loadKeystore( InputStream keyStream, + String passphrase, + String keystoreFormat ) + throws GeneralSecurityException, IOException + { + final KeyStore ks = KeyStore.getInstance( keystoreFormat ); + + if ( passphrase != null ) + { + final char [] passChars = passphrase.toCharArray(); + try + { + ks.load( keyStream, passChars ); + } + finally + { + Arrays.fill( passChars, (char) 0 ); + } + } + else + { + ks.load( keyStream, null ); + } + + return ks; + } +} 1.22 +301 -301 jakarta-avalon-cornerstone/src/java/org/apache/avalon/cornerstone/blocks/transport/publishing/AbstractPublisher.java Index: AbstractPublisher.java =================================================================== RCS file: /home/cvs/jakarta-avalon-cornerstone/src/java/org/apache/avalon/cornerstone/blocks/transport/publishing/AbstractPublisher.java,v retrieving revision 1.21 retrieving revision 1.22 diff -u -r1.21 -r1.22 --- AbstractPublisher.java 2 Oct 2002 01:52:16 -0000 1.21 +++ AbstractPublisher.java 23 Dec 2002 17:52:40 -0000 1.22 @@ -1,301 +1,301 @@ -/* - * Copyright (C) The Apache Software Foundation. All rights reserved. - * - * This software is published under the terms of the Apache Software License - * version 1.1, a copy of which has been included with this distribution in - * the LICENSE.txt file. - */ -package org.apache.avalon.cornerstone.blocks.transport.publishing; - -import java.io.File; -import java.net.MalformedURLException; -import java.net.URL; -import java.util.StringTokenizer; -import java.util.Vector; -import org.apache.avalon.framework.activity.Initializable; -import org.apache.avalon.framework.activity.Startable; -import org.apache.avalon.framework.configuration.Configurable; -import org.apache.avalon.framework.configuration.Configuration; -import org.apache.avalon.framework.configuration.ConfigurationException; -import org.apache.avalon.framework.context.Context; -import org.apache.avalon.framework.context.Contextualizable; -import org.apache.avalon.framework.logger.AbstractLogEnabled; -import org.apache.avalon.framework.service.ServiceException; -import org.apache.avalon.framework.service.ServiceManager; -import org.apache.avalon.framework.service.Serviceable; -import org.apache.avalon.phoenix.BlockContext; -import org.apache.excalibur.altrmi.common.MethodRequest; -import org.apache.excalibur.altrmi.server.AltrmiAuthenticator; -import org.apache.excalibur.altrmi.server.AltrmiPublisher; -import org.apache.excalibur.altrmi.server.ClassRetriever; -import org.apache.excalibur.altrmi.server.MethodInvocationHandler; -import org.apache.excalibur.altrmi.server.PublicationDescription; -import org.apache.excalibur.altrmi.server.PublicationException; -import org.apache.excalibur.altrmi.server.impl.AbstractServer; -import org.apache.excalibur.altrmi.server.impl.classretrievers.AbstractDynamicGeneratorClassRetriever; -import org.apache.excalibur.altrmi.server.impl.classretrievers.BcelDynamicGeneratorClassRetriever; -import org.apache.excalibur.altrmi.server.impl.classretrievers.JarFileClassRetriever; -import org.apache.excalibur.altrmi.server.impl.classretrievers.NoClassRetriever; - -/** - * Abstract Publisher. - * - * @author Paul Hammant Paul_Hammant@yahoo.com - * @author Thomas Kiesgen - * @version $Revision$ - */ -public abstract class AbstractPublisher - extends AbstractLogEnabled - implements AltrmiPublisher, Startable, Serviceable, Contextualizable, Configurable, - Initializable -{ - private AbstractServer m_abstractServer; - private ClassRetriever m_classRetriever; - private AltrmiAuthenticator m_altrmiAuthenticator; - protected File m_baseDirectory; - private boolean m_isDynamicPublisher = false; - - /** - * - * @param configuration - * @throws ConfigurationException - */ - public void configure( Configuration configuration ) - throws ConfigurationException - { - String classRetrieverType = configuration.getChild( "classRetrieverType" ).getValue(); - - if( classRetrieverType.equals( "jarFile" ) ) - { - StringTokenizer st = - new StringTokenizer( configuration.getChild( "gerneratedClassJarURLs" ).getValue(), - "," ); - Vector vector = new Vector(); - - while( st.hasMoreTokens() ) - { - try - { - String url = st.nextToken(); - - if( url.startsWith( "./" ) ) - { - File file = new File( m_baseDirectory, url.substring( 2, url.length() ) ); - - vector.add( file.toURL() ); - } - else - { - vector.add( new URL( url ) ); - } - } - catch( MalformedURLException e ) - { - getLogger() - .debug( "Can't create URL from config element 'gerneratedClassJarURLs'", - e ); - } - } - - URL[] urls = new URL[ vector.size() ]; - - vector.copyInto( urls ); - - m_classRetriever = new JarFileClassRetriever( urls ); - } - else if( classRetrieverType.equals( "none" ) ) - { - m_classRetriever = new NoClassRetriever(); - } - else if( classRetrieverType.equals( "bcel" ) ) - { - AbstractDynamicGeneratorClassRetriever generator = new BcelDynamicGeneratorClassRetriever(); - File classGenDir = new File( m_baseDirectory, configuration.getChild( "classGenDir" ).getValue( "" ) ); - generator.setClassGenDir( classGenDir.getAbsolutePath() ); - m_classRetriever = generator; - - m_isDynamicPublisher = true; - getLogger().debug( "setting classgen dir for generator to " + classGenDir.getAbsolutePath() ); - getLogger().debug( "setting class retriever to bcel dynamic generator" ); - } - - - else - { - throw new ConfigurationException( - "classRetrieverType must be 'bcel', 'jarFile' or 'none'" ); - } - } - - /** - * contextualize as per Contextualizable interface - * @param context - */ - public void contextualize( final Context context ) - { - m_baseDirectory = ( (BlockContext)context ).getBaseDirectory(); - } - - /** - * Service as per Serviceable interface - * @param manager a service manager - * @throws ServiceException if a problem during servicing - * @phoenix:dependency name="org.apache.excalibur.altrmi.server.AltrmiAuthenticator" - */ - public void service( ServiceManager manager ) - throws ServiceException - { - m_altrmiAuthenticator = - (AltrmiAuthenticator)manager.lookup( AltrmiAuthenticator.class.getName() ); - } - - /** - * initialize as per Initializable interface - * @throws Exception - */ - public void initialize() throws Exception - { - m_abstractServer.setClassRetriever( m_classRetriever ); - m_abstractServer.setAuthenticator( m_altrmiAuthenticator ); - } - - /** - * - * @param implementation - * @param asName - * @param interfaceToExpose - * @throws PublicationException - */ - public void publish( Object implementation, String asName, Class interfaceToExpose ) - throws PublicationException - { - if( getLogger().isDebugEnabled() ) - getLogger().debug( "Publishing object [as: " + asName + ", impl: " + implementation - + ", interf: "+ interfaceToExpose + "]" ); - - if( m_isDynamicPublisher ) - { - ( ( AbstractDynamicGeneratorClassRetriever ) m_classRetriever ).generate( asName, interfaceToExpose, this.getClass().getClassLoader() ); - if( getLogger().isDebugEnabled() ) - { - getLogger().debug( "generated dynamic proxy for published interface " + asName ); - } - } - - m_abstractServer.publish( implementation, asName, interfaceToExpose ); - } - - /** - * Publish an service - * @param implementation - * @param asName - * @param publicationDescription - * @throws PublicationException - */ - public void publish( - Object implementation, String asName, PublicationDescription publicationDescription ) - throws PublicationException - { - if( getLogger().isDebugEnabled() ) - getLogger().debug( "Publishing object [as: " + asName + ", impl: " + implementation + "]" ); - - if( m_isDynamicPublisher ) - { - ( ( AbstractDynamicGeneratorClassRetriever ) m_classRetriever ).generate( asName, publicationDescription, this.getClass().getClassLoader() ); - if( getLogger().isDebugEnabled() ) - { - getLogger().debug( "generated dynamic proxy for published interface " + asName ); - } - } - - - - m_abstractServer.publish( implementation, asName, publicationDescription ); - } - - /** - * - * @param object - * @param asName - * @throws PublicationException - */ - public void unPublish( Object object, String asName ) throws PublicationException - { - if( getLogger().isDebugEnabled() ) - getLogger().debug( "Unpublishing object [nane: " + asName + ", impl: " + object + "]" ); - - m_abstractServer.unPublish( object, asName ); - } - - /** - * - * @param object - * @param asName - * @param o1 - * @throws PublicationException - */ - public void replacePublished( Object object, String asName, Object o1 ) throws PublicationException - { - if( getLogger().isDebugEnabled() ) - getLogger().debug( "Replacing published object [nane: " + asName + ", existing: " + object + ", new: " + o1 + "]" ); - - m_abstractServer.replacePublished( object, asName, o1 ); - } - - /** - * - * @throws Exception - */ - public void start() throws Exception - { - m_abstractServer.start(); - } - - /** - * - * @throws Exception - */ - public void stop() throws Exception - { - m_abstractServer.stop(); - } - - /** - * - * @param request - * @param publishedName - * @return - */ - public MethodInvocationHandler getMethodInvocationHandler( MethodRequest request, String publishedName ) - { - return m_abstractServer.getMethodInvocationHandler( request, publishedName ); - } - - /** - * - * @param publishedName - * @return - */ - public MethodInvocationHandler getMethodInvocationHandler(String publishedName) - { - return m_abstractServer.getMethodInvocationHandler( publishedName ); - } - - /** - * - * @return - */ - protected AbstractServer getAbstractServer() - { - return m_abstractServer; - } - - /** - * - * @param abstractServer - */ - protected void setAbstractServer( AbstractServer abstractServer ) - { - m_abstractServer = abstractServer; - } -} +/* + * Copyright (C) The Apache Software Foundation. All rights reserved. + * + * This software is published under the terms of the Apache Software License + * version 1.1, a copy of which has been included with this distribution in + * the LICENSE.txt file. + */ +package org.apache.avalon.cornerstone.blocks.transport.publishing; + +import java.io.File; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.StringTokenizer; +import java.util.Vector; +import org.apache.avalon.framework.activity.Initializable; +import org.apache.avalon.framework.activity.Startable; +import org.apache.avalon.framework.configuration.Configurable; +import org.apache.avalon.framework.configuration.Configuration; +import org.apache.avalon.framework.configuration.ConfigurationException; +import org.apache.avalon.framework.context.Context; +import org.apache.avalon.framework.context.Contextualizable; +import org.apache.avalon.framework.logger.AbstractLogEnabled; +import org.apache.avalon.framework.service.ServiceException; +import org.apache.avalon.framework.service.ServiceManager; +import org.apache.avalon.framework.service.Serviceable; +import org.apache.excalibur.altrmi.common.MethodRequest; +import org.apache.excalibur.altrmi.server.AltrmiAuthenticator; +import org.apache.excalibur.altrmi.server.AltrmiPublisher; +import org.apache.excalibur.altrmi.server.ClassRetriever; +import org.apache.excalibur.altrmi.server.MethodInvocationHandler; +import org.apache.excalibur.altrmi.server.PublicationDescription; +import org.apache.excalibur.altrmi.server.PublicationException; +import org.apache.excalibur.altrmi.server.impl.AbstractServer; +import org.apache.excalibur.altrmi.server.impl.classretrievers.AbstractDynamicGeneratorClassRetriever; +import org.apache.excalibur.altrmi.server.impl.classretrievers.BcelDynamicGeneratorClassRetriever; +import org.apache.excalibur.altrmi.server.impl.classretrievers.JarFileClassRetriever; +import org.apache.excalibur.altrmi.server.impl.classretrievers.NoClassRetriever; +import org.apache.avalon.framework.context.ContextException; + +/** + * Abstract Publisher. + * + * @author Paul Hammant Paul_Hammant@yahoo.com + * @author Thomas Kiesgen + * @version $Revision$ + */ +public abstract class AbstractPublisher + extends AbstractLogEnabled + implements AltrmiPublisher, Startable, Serviceable, Contextualizable, Configurable, + Initializable +{ + private AbstractServer m_abstractServer; + private ClassRetriever m_classRetriever; + private AltrmiAuthenticator m_altrmiAuthenticator; + protected File m_baseDirectory; + private boolean m_isDynamicPublisher = false; + + /** + * + * @param configuration + * @throws ConfigurationException + */ + public void configure( Configuration configuration ) + throws ConfigurationException + { + String classRetrieverType = configuration.getChild( "classRetrieverType" ).getValue(); + + if( classRetrieverType.equals( "jarFile" ) ) + { + StringTokenizer st = + new StringTokenizer( configuration.getChild( "gerneratedClassJarURLs" ).getValue(), + "," ); + Vector vector = new Vector(); + + while( st.hasMoreTokens() ) + { + try + { + String url = st.nextToken(); + + if( url.startsWith( "./" ) ) + { + File file = new File( m_baseDirectory, url.substring( 2, url.length() ) ); + + vector.add( file.toURL() ); + } + else + { + vector.add( new URL( url ) ); + } + } + catch( MalformedURLException e ) + { + getLogger() + .debug( "Can't create URL from config element 'gerneratedClassJarURLs'", + e ); + } + } + + URL[] urls = new URL[ vector.size() ]; + + vector.copyInto( urls ); + + m_classRetriever = new JarFileClassRetriever( urls ); + } + else if( classRetrieverType.equals( "none" ) ) + { + m_classRetriever = new NoClassRetriever(); + } + else if( classRetrieverType.equals( "bcel" ) ) + { + AbstractDynamicGeneratorClassRetriever generator = new BcelDynamicGeneratorClassRetriever(); + File classGenDir = new File( m_baseDirectory, configuration.getChild( "classGenDir" ).getValue( "" ) ); + generator.setClassGenDir( classGenDir.getAbsolutePath() ); + m_classRetriever = generator; + + m_isDynamicPublisher = true; + getLogger().debug( "setting classgen dir for generator to " + classGenDir.getAbsolutePath() ); + getLogger().debug( "setting class retriever to bcel dynamic generator" ); + } + + + else + { + throw new ConfigurationException( + "classRetrieverType must be 'bcel', 'jarFile' or 'none'" ); + } + } + + /** + * contextualize as per Contextualizable interface + * @param context + */ + public void contextualize( final Context context ) throws ContextException + { + m_baseDirectory = ( File ) context.get("app.home"); + } + + /** + * Service as per Serviceable interface + * @param manager a service manager + * @throws ServiceException if a problem during servicing + * @phoenix:dependency name="org.apache.excalibur.altrmi.server.AltrmiAuthenticator" + */ + public void service( ServiceManager manager ) + throws ServiceException + { + m_altrmiAuthenticator = + (AltrmiAuthenticator)manager.lookup( AltrmiAuthenticator.class.getName() ); + } + + /** + * initialize as per Initializable interface + * @throws Exception + */ + public void initialize() throws Exception + { + m_abstractServer.setClassRetriever( m_classRetriever ); + m_abstractServer.setAuthenticator( m_altrmiAuthenticator ); + } + + /** + * + * @param implementation + * @param asName + * @param interfaceToExpose + * @throws PublicationException + */ + public void publish( Object implementation, String asName, Class interfaceToExpose ) + throws PublicationException + { + if( getLogger().isDebugEnabled() ) + getLogger().debug( "Publishing object [as: " + asName + ", impl: " + implementation + + ", interf: "+ interfaceToExpose + "]" ); + + if( m_isDynamicPublisher ) + { + ( ( AbstractDynamicGeneratorClassRetriever ) m_classRetriever ).generate( asName, interfaceToExpose, this.getClass().getClassLoader() ); + if( getLogger().isDebugEnabled() ) + { + getLogger().debug( "generated dynamic proxy for published interface " + asName ); + } + } + + m_abstractServer.publish( implementation, asName, interfaceToExpose ); + } + + /** + * Publish an service + * @param implementation + * @param asName + * @param publicationDescription + * @throws PublicationException + */ + public void publish( + Object implementation, String asName, PublicationDescription publicationDescription ) + throws PublicationException + { + if( getLogger().isDebugEnabled() ) + getLogger().debug( "Publishing object [as: " + asName + ", impl: " + implementation + "]" ); + + if( m_isDynamicPublisher ) + { + ( ( AbstractDynamicGeneratorClassRetriever ) m_classRetriever ).generate( asName, publicationDescription, this.getClass().getClassLoader() ); + if( getLogger().isDebugEnabled() ) + { + getLogger().debug( "generated dynamic proxy for published interface " + asName ); + } + } + + + + m_abstractServer.publish( implementation, asName, publicationDescription ); + } + + /** + * + * @param object + * @param asName + * @throws PublicationException + */ + public void unPublish( Object object, String asName ) throws PublicationException + { + if( getLogger().isDebugEnabled() ) + getLogger().debug( "Unpublishing object [nane: " + asName + ", impl: " + object + "]" ); + + m_abstractServer.unPublish( object, asName ); + } + + /** + * + * @param object + * @param asName + * @param o1 + * @throws PublicationException + */ + public void replacePublished( Object object, String asName, Object o1 ) throws PublicationException + { + if( getLogger().isDebugEnabled() ) + getLogger().debug( "Replacing published object [nane: " + asName + ", existing: " + object + ", new: " + o1 + "]" ); + + m_abstractServer.replacePublished( object, asName, o1 ); + } + + /** + * + * @throws Exception + */ + public void start() throws Exception + { + m_abstractServer.start(); + } + + /** + * + * @throws Exception + */ + public void stop() throws Exception + { + m_abstractServer.stop(); + } + + /** + * + * @param request + * @param publishedName + * @return + */ + public MethodInvocationHandler getMethodInvocationHandler( MethodRequest request, String publishedName ) + { + return m_abstractServer.getMethodInvocationHandler( request, publishedName ); + } + + /** + * + * @param publishedName + * @return + */ + public MethodInvocationHandler getMethodInvocationHandler(String publishedName) + { + return m_abstractServer.getMethodInvocationHandler( publishedName ); + } + + /** + * + * @return + */ + protected AbstractServer getAbstractServer() + { + return m_abstractServer; + } + + /** + * + * @param abstractServer + */ + protected void setAbstractServer( AbstractServer abstractServer ) + { + m_abstractServer = abstractServer; + } +} -- To unsubscribe, e-mail: