KickJava   Java API By Example, From Geeks To Geeks.

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


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.InterceptorStack;
21 import org.apache.hivemind.Location;
22 import org.apache.hivemind.Orderable;
23 import org.apache.hivemind.ServiceInterceptorFactory;
24 import org.apache.hivemind.definition.ServicePointDefinition;
25 import org.apache.hivemind.internal.AbstractServiceInterceptorConstructor;
26 import org.apache.hivemind.internal.Module;
27 import org.apache.hivemind.internal.ServicePoint;
28 import org.apache.hivemind.schema.Schema;
29 import org.apache.hivemind.util.Defense;
30 import org.apache.hivemind.util.InstanceCreationUtils;
31 import org.apache.hivemind.xml.definition.impl.XmlServicePointDefinitionImpl;
32
33 /**
34  * Constructs a new interceptor by invoking methods on another service (which implements the
35  * {@link org.apache.hivemind.ServiceInterceptorFactory} interface.
36  */

37 public final class InvokeFactoryInterceptorConstructor extends AbstractServiceInterceptorConstructor implements
38         Orderable
39 {
40     private String JavaDoc _factoryServiceId;
41     
42     private ServiceInterceptorFactory _factory;
43
44     /** List of {@link org.apache.hivemind.Element}, the raw XML parameters. */
45     private List JavaDoc _parameters;
46
47     /** The parameters converted to objects as per the factory's parameter schema. */
48     private Object JavaDoc _convertedParameters;
49
50     private String JavaDoc _precedingInterceptorIds;
51
52     private String JavaDoc _followingInterceptorIds;
53     
54     public InvokeFactoryInterceptorConstructor(Location location)
55     {
56         super(location);
57     }
58
59     /**
60      * @return Returns the name.
61      */

62     public String JavaDoc getName()
63     {
64         return _factoryServiceId;
65     }
66     
67     public void constructServiceInterceptor(InterceptorStack stack, Module contributingModule)
68     {
69         setup(contributingModule);
70
71         _factory.createInterceptor(stack, contributingModule, _convertedParameters);
72     }
73
74     // A lot of changes to synchronization and service construction occured between 1.1 and 1.1.1;
75
// this method was split off and made synchronized ... otherwise, it was possible for the
76
// pooled or threaded services to get into a potential race condition through this code.
77

78     private synchronized void setup(Module contributingModule)
79     {
80         if (_factory == null)
81         {
82             ServicePoint factoryPoint = contributingModule.getServicePoint(_factoryServiceId);
83
84             _factory = (ServiceInterceptorFactory) factoryPoint
85                     .getService(ServiceInterceptorFactory.class);
86
87             ServicePointDefinition spd = factoryPoint.getServicePointDefinition();
88             if (!(spd instanceof XmlServicePointDefinitionImpl)) {
89                 // TODO annotations: Externalize message
90
throw new ApplicationRuntimeException("ServicePoint used as InterceptorFactory must be of type XmlServicePointDefinitionImpl");
91             }
92             XmlServicePointDefinitionImpl xmlServicePoint = (XmlServicePointDefinitionImpl) spd;
93             Schema schema = xmlServicePoint.getParametersSchema();
94             if (schema != null) {
95
96                 SchemaProcessorImpl processor = new SchemaProcessorImpl(factoryPoint.getErrorLog(),
97                         schema);
98                 
99                 _convertedParameters = constructParametersContainer(schema.getRootElementClassName(), factoryPoint.getModule());
100                 processor.process(_convertedParameters, _parameters, contributingModule);
101             }
102         }
103     }
104
105     public List JavaDoc getParameters()
106     {
107         return _parameters;
108     }
109
110     public void setParameters(List JavaDoc list)
111     {
112         _parameters = list;
113     }
114
115     public void setFactoryServiceId(String JavaDoc string)
116     {
117         _factoryServiceId = string;
118     }
119     
120     public Object JavaDoc constructParametersContainer(String JavaDoc containerClassName, Module definingModule)
121     {
122         Defense.notNull(containerClassName, "containerClassName");
123         
124         return InstanceCreationUtils.createInstance(
125                 definingModule,
126                 containerClassName,
127                 getLocation());
128     }
129
130     public void setFollowingInterceptorIds(String JavaDoc ids)
131     {
132         _followingInterceptorIds = ids;
133     }
134
135     public void setPrecedingInterceptorIds(String JavaDoc ids)
136     {
137         _precedingInterceptorIds = ids;
138     }
139
140     public String JavaDoc getFollowingNames()
141     {
142         return _followingInterceptorIds;
143     }
144
145     public String JavaDoc getPrecedingNames()
146     {
147         return _precedingInterceptorIds;
148     }
149 }
Popular Tags