View Javadoc

1   package org.apache.maven.continuum.xmlrpc.server;
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 org.apache.xmlrpc.XmlRpcException;
23  import org.apache.xmlrpc.XmlRpcRequest;
24  import org.apache.xmlrpc.server.RequestProcessorFactoryFactory;
25  import org.codehaus.plexus.PlexusConstants;
26  import org.codehaus.plexus.PlexusContainer;
27  import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
28  import org.codehaus.plexus.context.Context;
29  import org.codehaus.plexus.context.ContextException;
30  import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
31  import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
32  import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
33  import org.slf4j.Logger;
34  import org.slf4j.LoggerFactory;
35  
36  import java.util.HashMap;
37  import java.util.Map;
38  
39  /**
40   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
41   * @version $Id: ConfiguredBeanProcessorFactory.java 1372260 2012-08-13 04:29:09Z brett $
42   * @plexus.component role="org.apache.xmlrpc.server.RequestProcessorFactoryFactory"
43   */
44  public class ConfiguredBeanProcessorFactory
45      implements RequestProcessorFactoryFactory, Initializable, Contextualizable
46  {
47      private static final Logger log = LoggerFactory.getLogger( ConfiguredBeanProcessorFactory.class );
48  
49      /**
50       * @plexus.requirement role="org.apache.maven.continuum.xmlrpc.server.ContinuumXmlRpcComponent"
51       */
52      private Map<String, Object> xmlrpcComponents;
53  
54      private Map<String, String> componentsMapping = new HashMap<String, String>();
55  
56      PlexusContainer container;
57  
58      public void initialize()
59          throws InitializationException
60      {
61          for ( String key : xmlrpcComponents.keySet() )
62          {
63              String className = xmlrpcComponents.get( key ).getClass().getName();
64              componentsMapping.put( className, key );
65          }
66      }
67  
68      public RequestProcessorFactory getRequestProcessorFactory( final Class cls )
69          throws XmlRpcException
70      {
71          return new RequestProcessorFactory()
72          {
73              public Object getRequestProcessor( XmlRpcRequest request )
74                  throws XmlRpcException
75              {
76                  Object obj = ConfiguredBeanProcessorFactory.this.getRequestProcessor( cls );
77  
78                  if ( obj instanceof ContinuumXmlRpcComponent )
79                  {
80                      ContinuumXmlRpcConfig config = (ContinuumXmlRpcConfig) request.getConfig();
81                      ( (ContinuumXmlRpcComponent) obj ).setConfig( config );
82                  }
83                  return obj;
84              }
85          };
86      }
87  
88      protected Object getRequestProcessor( Class cls )
89          throws XmlRpcException
90      {
91          log.debug( "Load '" + cls.getName() + "' handler." );
92  
93          Object obj = null;
94          try
95          {
96              obj = getComponent( cls );
97          }
98          catch ( ComponentLookupException e )
99          {
100             log.error( "Can't load component.", e );
101         }
102 
103         if ( obj == null )
104         {
105             throw new XmlRpcException( "Handler bean not found for: " + cls );
106         }
107 
108         return obj;
109     }
110 
111     private String getComponentKey( Class cls )
112     {
113         return componentsMapping.get( cls.getName() );
114     }
115 
116     private Object getComponent( Class cls )
117         throws ComponentLookupException
118     {
119         String key = getComponentKey( cls );
120         log.debug( "load component:" + key );
121         return container.lookup( ContinuumXmlRpcComponent.class.getName(), key );
122     }
123 
124     public void contextualize( Context context )
125         throws ContextException
126     {
127         container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
128     }
129 }