001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: WebservicesHessianDeployer.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.engine.elements;
009:
010: import com.caucho.hessian.io.SerializerFactory;
011: import com.caucho.hessian.server.HessianSkeleton;
012: import com.caucho.services.server.GenericService;
013: import com.caucho.services.server.Service;
014: import com.uwyn.rife.engine.ElementDeployer;
015: import com.uwyn.rife.engine.exceptions.EngineException;
016: import com.uwyn.rife.engine.exceptions.PropertyRequiredException;
017: import com.uwyn.rife.tools.IteratorEnumeration;
018: import java.util.Enumeration;
019: import javax.servlet.ServletConfig;
020: import javax.servlet.ServletContext;
021: import javax.servlet.ServletException;
022:
023: public class WebservicesHessianDeployer extends ElementDeployer {
024: private Class mHomeAPI;
025: private Object mHomeImpl;
026:
027: private Class mObjectAPI;
028: private Object mObjectImpl;
029:
030: private HessianSkeleton mHomeSkeleton;
031: private HessianSkeleton mObjectSkeleton;
032:
033: private SerializerFactory mSerializerFactory;
034:
035: public WebservicesHessianDeployer() {
036: }
037:
038: public void deploy() throws EngineException {
039: try {
040: if (mHomeImpl != null) {
041: } else if (getElementInfo().containsProperty("home-class")) {
042: String class_name = getElementInfo().getPropertyString(
043: "home-class");
044:
045: Class home_class = loadClass(class_name);
046:
047: mHomeImpl = home_class.newInstance();
048:
049: init(mHomeImpl);
050: } else if (getElementInfo().containsProperty(
051: "service-class")) {
052: String class_name = getElementInfo().getPropertyString(
053: "service-class");
054:
055: Class home_class = loadClass(class_name);
056:
057: mHomeImpl = home_class.newInstance();
058:
059: init(mHomeImpl);
060: } else {
061: if (!getElementInfo().containsProperty("service-class")) {
062: throw new PropertyRequiredException(
063: getElementInfo().getDeclarationName(),
064: "service-class");
065: }
066: }
067:
068: if (mHomeAPI != null) {
069: } else if (getElementInfo().containsProperty("home-api")) {
070: String class_name = getElementInfo().getPropertyString(
071: "home-api");
072:
073: mHomeAPI = loadClass(class_name);
074: } else if (getElementInfo().containsProperty("api-class")) {
075: String class_name = getElementInfo().getPropertyString(
076: "api-class");
077:
078: mHomeAPI = loadClass(class_name);
079: } else if (mHomeImpl != null) {
080: mHomeAPI = findRemoteAPI(mHomeImpl.getClass());
081:
082: if (mHomeAPI == null) {
083: mHomeAPI = mHomeImpl.getClass();
084: }
085: }
086:
087: if (mObjectImpl != null) {
088: } else if (getElementInfo()
089: .containsProperty("object-class")) {
090: String class_name = getElementInfo().getPropertyString(
091: "object-class");
092:
093: Class object_class = loadClass(class_name);
094:
095: mObjectImpl = object_class.newInstance();
096:
097: init(mObjectImpl);
098: }
099:
100: if (mObjectAPI != null) {
101: } else if (getElementInfo().containsProperty("object-api")) {
102: String class_name = getElementInfo().getPropertyString(
103: "object-api");
104:
105: mObjectAPI = loadClass(class_name);
106: } else if (mObjectImpl != null) {
107: mObjectAPI = mObjectImpl.getClass();
108: }
109:
110: mHomeSkeleton = new HessianSkeleton(mHomeImpl, mHomeAPI);
111: if (mObjectAPI != null) {
112: mHomeSkeleton.setObjectClass(mObjectAPI);
113: }
114:
115: if (mObjectImpl != null) {
116: mObjectSkeleton = new HessianSkeleton(mObjectImpl,
117: mObjectAPI);
118: mObjectSkeleton.setHomeClass(mHomeAPI);
119: } else {
120: mObjectSkeleton = mHomeSkeleton;
121: }
122: } catch (Exception e) {
123: throw new EngineException(e);
124: }
125: }
126:
127: private Class findRemoteAPI(Class implClass) {
128: if (implClass == null || implClass.equals(GenericService.class)) {
129: return null;
130: }
131:
132: Class interfaces[] = implClass.getInterfaces();
133:
134: if (interfaces.length == 1) {
135: return interfaces[0];
136: }
137:
138: return findRemoteAPI(implClass.getSuperclass());
139: }
140:
141: private Class loadClass(String className)
142: throws ClassNotFoundException {
143: ClassLoader loader = Thread.currentThread()
144: .getContextClassLoader();
145:
146: if (loader != null) {
147: return Class.forName(className, false, loader);
148: } else {
149: return Class.forName(className);
150: }
151: }
152:
153: private void init(Object service) throws ServletException {
154: if (service instanceof Service) {
155: ((Service) service).init(new ServletConfig() {
156: public String getServletName() {
157: return getElementInfo().getDeclarationName();
158: }
159:
160: public ServletContext getServletContext() {
161: return null;
162: }
163:
164: public String getInitParameter(String key) {
165: return getElementInfo().getPropertyString(key);
166: }
167:
168: public Enumeration getInitParameterNames() {
169: return new IteratorEnumeration(getElementInfo()
170: .getPropertyNames().iterator());
171: }
172: });
173: }
174: }
175:
176: /**
177: * Sets the home skeleton.
178: */
179: public HessianSkeleton getHomeSkeleton() {
180: return mHomeSkeleton;
181: }
182:
183: /**
184: * Sets the object skeleton.
185: */
186: public HessianSkeleton getObjectSkeleton() {
187: return mObjectSkeleton;
188: }
189:
190: /**
191: * Sets the home api.
192: */
193: public void setHomeAPI(Class api) {
194: mHomeAPI = api;
195: }
196:
197: /**
198: * Sets the home implementation
199: */
200: public void setHome(Object home) {
201: mHomeImpl = home;
202: }
203:
204: /**
205: * Gets the home implementation
206: */
207: Object getHome() {
208: return mHomeImpl;
209: }
210:
211: /**
212: * Sets the object api.
213: */
214: public void setObjectAPI(Class api) {
215: mObjectAPI = api;
216: }
217:
218: /**
219: * Sets the object implementation
220: */
221: public void setObject(Object object) {
222: mObjectImpl = object;
223: }
224:
225: /**
226: * Gets the object implementation
227: */
228: Object getObject() {
229: return mObjectImpl;
230: }
231:
232: /**
233: * Sets the service class.
234: */
235: public void setService(Object service) {
236: setHome(service);
237: }
238:
239: /**
240: * Sets the api-class.
241: */
242: public void setAPIClass(Class api) {
243: setHomeAPI(api);
244: }
245:
246: /**
247: * Gets the api-class.
248: */
249: public Class getAPIClass() {
250: return mHomeAPI;
251: }
252:
253: /**
254: * Sets the serializer factory.
255: */
256: public void setSerializerFactory(SerializerFactory factory) {
257: mSerializerFactory = factory;
258: }
259:
260: /**
261: * Gets the serializer factory.
262: */
263: public SerializerFactory getSerializerFactory() {
264: if (mSerializerFactory == null) {
265: mSerializerFactory = new SerializerFactory();
266: }
267:
268: return mSerializerFactory;
269: }
270:
271: /**
272: * Sets the serializer send collection java type.
273: */
274: public void setSendCollectionType(boolean sendType) {
275: getSerializerFactory().setSendCollectionType(sendType);
276: }
277: }
|