001: /*
002: * (C) Copyright 2000- 2005 Nabh Information Systems, Inc.
003: *
004: * This program is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU General Public License
006: * as published by the Free Software Foundation; either version 2
007: * of the License, or (at your option) any later version.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: *
018: */
019: package com.nabhinc.ws.service.core;
020:
021: import java.lang.reflect.InvocationTargetException;
022:
023: import com.nabhinc.util.ReflectionUtil;
024: import com.nabhinc.ws.core.PropertyInfo;
025: import com.nabhinc.ws.core.UnavailableException;
026: import com.nabhinc.ws.core.WebServiceException;
027: import com.nabhinc.ws.server.Current;
028: import com.nabhinc.ws.server.RequestInfo;
029: import com.nabhinc.ws.server.WebService;
030: import com.nabhinc.ws.server.WebServiceImpl;
031:
032: /**
033: * Root class for all web service implementations that use a Java
034: * delegate object for executing Web service operations. Subclasses must
035: * override <code>getImplementationObject()</code> method to provide
036: * the Java object implementing service functionality.
037: *
038: * @author Padmanabh Dabke
039: * (c) 2005 Nabh Information Systems, Inc. All Rights Reserved.
040: */
041: public abstract class JavaDelegateWebService extends WebServiceImpl
042: implements WebService {
043:
044: public static final int SCOPE_APPLICATION = 0;
045: public static final int SCOPE_SERVICE = 1;
046: public static final int SCOPE_SESSION = 2;
047: public static final int SCOPE_REQUEST = 3;
048: public static final String DEFAULT_SCOPE = "service";
049: public static final String DEFAULT_DELEGATE_ATTRIB_NAME = "stringbeans.ws.delegate_attribute";
050: protected String jdwsScope = DEFAULT_SCOPE;
051: protected String jdwsAttributeName = DEFAULT_DELEGATE_ATTRIB_NAME;
052:
053: protected transient Object _jdwsJavaObject = null;
054: protected transient int _jdwsScope = SCOPE_SERVICE;
055:
056: public void setScope(String scope) {
057: jdwsScope = scope;
058: }
059:
060: public void setAttributeName(String attrib) {
061: jdwsAttributeName = attrib;
062: }
063:
064: public void setProperties(PropertyInfo[] props)
065: throws WebServiceException {
066: super .setProperties(props);
067: checkProperties();
068: if (_jdwsScope == SCOPE_SERVICE) {
069: _jdwsJavaObject = createDelegateInstance();
070: }
071: }
072:
073: protected void checkProperties() throws WebServiceException {
074: if (jdwsScope.equals("service")) {
075: _jdwsScope = SCOPE_SERVICE;
076: } else if (jdwsScope.equals("session"))
077: _jdwsScope = SCOPE_SESSION;
078: else if (jdwsScope.equals("request"))
079: _jdwsScope = SCOPE_REQUEST;
080: else if (jdwsScope.equals("application"))
081: _jdwsScope = SCOPE_APPLICATION;
082: else
083: throw new WebServiceException("Invalid scope: " + jdwsScope);
084:
085: }
086:
087: /**
088: * Invokes a method on delegate object.
089: */
090: public Object invoke(RequestInfo reqInfo)
091: throws WebServiceException {
092: try {
093: Object delegate = getDelegate();
094: if (reqInfo.method == null) {
095: try {
096: reqInfo.method = ReflectionUtil.getMethod(delegate,
097: reqInfo.methodName, reqInfo.arguments);
098: } catch (Exception ex) {
099: reqInfo.error = new UnavailableException(
100: "Failed to find method "
101: + reqInfo.methodName
102: + " on target object of class "
103: + this .getClass().getName());
104: }
105: }
106: return reqInfo.method.invoke(delegate, reqInfo.arguments);
107:
108: } catch (InvocationTargetException ex) {
109: throw new WebServiceException(ex.getMessage(), ex
110: .getCause());
111: } catch (IllegalAccessException e) {
112: throw new UnavailableException("Object method "
113: + reqInfo.methodName + " is inaccessible.");
114: }
115:
116: }
117:
118: /* (non-Javadoc)
119: * @see com.nabhinc.ws.server.WebService#getServiceInstance()
120: */
121: protected Object getDelegate() throws WebServiceException {
122: switch (_jdwsScope) {
123: case SCOPE_SERVICE:
124: return _jdwsJavaObject;
125: case SCOPE_REQUEST:
126: try {
127: return createDelegateInstance();
128: } catch (Exception e) {
129: throw new WebServiceException(
130: "Failed to instantiate Java object.", e);
131: }
132: case SCOPE_SESSION:
133: com.nabhinc.core.WebServiceSession session = Current
134: .getRequestContext().getWebServiceRequest()
135: .getSession();
136: Object obj = session.getAttribute(jdwsAttributeName);
137: if (obj == null) {
138: obj = createDelegateInstance();
139: session.setAttribute(jdwsAttributeName, obj);
140: return obj;
141: } else {
142: return obj;
143: }
144: case SCOPE_APPLICATION:
145: synchronized (this .getClass()) {
146: Object obj2 = soiServerContext
147: .getAttribute(jdwsAttributeName);
148: if (obj2 == null) {
149: obj2 = createDelegateInstance();
150: soiServerContext.setAttribute(jdwsAttributeName,
151: obj2);
152: return obj2;
153: } else {
154: return obj2;
155: }
156: }
157: }
158: throw new WebServiceException("Invalid scope.");
159: }
160:
161: /**
162: * All sub-classes should implement this method to return the delegate
163: * object implementing service operations.
164: * @return Java object implementing service operations.
165: * @throws WebServiceException
166: */
167: protected abstract Object createDelegateInstance()
168: throws WebServiceException;
169:
170: }
|