KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > impl > InvokeFactoryServiceConstructor


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.hivemind.impl;
16
17 import java.util.List JavaDoc;
18
19 import org.apache.hivemind.ApplicationRuntimeException;
20 import org.apache.hivemind.ErrorLog;
21 import org.apache.hivemind.Location;
22 import org.apache.hivemind.ServiceImplementationFactory;
23 import org.apache.hivemind.ServiceImplementationFactoryParameters;
24 import org.apache.hivemind.definition.ImplementationConstructionContext;
25 import org.apache.hivemind.definition.Occurances;
26 import org.apache.hivemind.definition.ServicePointDefinition;
27 import org.apache.hivemind.internal.AbstractServiceImplementationConstructor;
28 import org.apache.hivemind.internal.Module;
29 import org.apache.hivemind.internal.ServicePoint;
30 import org.apache.hivemind.schema.Schema;
31 import org.apache.hivemind.util.Defense;
32 import org.apache.hivemind.util.InstanceCreationUtils;
33 import org.apache.hivemind.xml.definition.impl.XmlServicePointDefinitionImpl;
34
35 /**
36  * Constructs a new service by invoking methods on another service (which implements the
37  * {@link org.apache.hivemind.ServiceImplementationFactory} interface.
38  *
39  * @author Howard Lewis Ship
40  */

41 public final class InvokeFactoryServiceConstructor extends AbstractServiceImplementationConstructor
42 {
43     private String JavaDoc _factoryServiceId;
44
45     /** List of {@link org.apache.hivemind.Element}, the raw XML parameters. */
46     private List JavaDoc _parameters;
47
48     /** The factory service to be invoked. */
49     private ServiceImplementationFactory _factory;
50
51     /** The parameters converted to objects as per the factory's parameter schema. */
52     private Object JavaDoc _convertedParameters;
53     
54     public InvokeFactoryServiceConstructor(Location location, String JavaDoc contributingModuleId)
55     {
56         super(location);
57     }
58
59     public Object JavaDoc constructCoreServiceImplementation(ImplementationConstructionContext context)
60     {
61         setupFactoryAndParameters(context.getServicePoint(), context.getDefiningModule());
62
63         try
64         {
65             ServiceImplementationFactoryParameters factoryParameters = new ServiceImplementationFactoryParametersImpl(
66                     context.getServicePoint(), context.getDefiningModule(), _convertedParameters);
67
68             return _factory.createCoreServiceImplementation(factoryParameters);
69         }
70         catch (Exception JavaDoc ex)
71         {
72             throw new ApplicationRuntimeException(ex.getMessage(), getLocation(), ex);
73         }
74     }
75
76     // A lot of changes to synchronization and service construction occured between 1.1 and 1.1.1;
77
// this method was split off and made synchronized ... otherwise, it was possible for the
78
// pooled or threaded services to get into a potential race condition through this code.
79

80     private synchronized void setupFactoryAndParameters(ServicePoint servicePoint, Module contributingModule)
81     {
82         if (_factory == null)
83         {
84             ServicePoint factoryPoint = contributingModule.getServicePoint(_factoryServiceId);
85
86             ErrorLog errorLog = servicePoint.getErrorLog();
87             
88             _factory = (ServiceImplementationFactory) factoryPoint
89                 .getService(ServiceImplementationFactory.class);
90
91             ServicePointDefinition spd = factoryPoint.getServicePointDefinition();
92             if (!(spd instanceof XmlServicePointDefinitionImpl)) {
93                 // TODO annotations: Externalize message
94
throw new ApplicationRuntimeException("ServicePoint used as ServiceImplementationFactory must be of type XmlServicePointDefinitionImpl");
95             }
96             XmlServicePointDefinitionImpl xmlServicePoint = (XmlServicePointDefinitionImpl) spd;
97
98             Schema schema = xmlServicePoint.getParametersSchema();
99             if (schema != null) {
100                 
101                 Occurances expected = xmlServicePoint.getParametersCount();
102                 checkParameterCounts(errorLog, expected);
103
104                 SchemaProcessorImpl processor = new SchemaProcessorImpl(errorLog, schema);
105                 
106                 _convertedParameters = constructParametersContainer(schema.getRootElementClassName(), factoryPoint.getModule());
107                 processor.process(_convertedParameters, _parameters, contributingModule);
108             }
109         }
110     }
111
112     public List JavaDoc getParameters()
113     {
114         return _parameters;
115     }
116
117     public void setParameters(List JavaDoc list)
118     {
119         _parameters = list;
120     }
121
122     public void setFactoryServiceId(String JavaDoc factoryServiceId)
123     {
124         _factoryServiceId = factoryServiceId;
125     }
126
127     public String JavaDoc getFactoryServiceId()
128     {
129         return _factoryServiceId;
130     }
131     
132     public Object JavaDoc constructParametersContainer(String JavaDoc containerClassName, Module definingModule)
133     {
134         Defense.notNull(containerClassName, "containerClassName");
135         
136         return InstanceCreationUtils.createInstance(
137                 definingModule,
138                 containerClassName,
139                 getLocation());
140     }
141     
142     /**
143      * Checks that the number of parameter elements matches the expected count.
144      */

145     private void checkParameterCounts(ErrorLog log, Occurances expected)
146     {
147         int actual = _parameters.size();
148
149         if (expected.inRange(actual))
150             return;
151
152         String JavaDoc message = XmlImplMessages.wrongNumberOfParameters(_factoryServiceId, actual, expected);
153
154         log.error(message, getLocation(), null);
155     }
156
157
158 }
Popular Tags