1 package org.apache.maven.continuum.xmlrpc.server;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
41
42
43
44 public class ConfiguredBeanProcessorFactory
45 implements RequestProcessorFactoryFactory, Initializable, Contextualizable
46 {
47 private static final Logger log = LoggerFactory.getLogger( ConfiguredBeanProcessorFactory.class );
48
49
50
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 }