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:
020: package com.nabhinc.ws.server;
021:
022: import java.io.IOException;
023: import java.io.Writer;
024:
025: import javax.servlet.ServletException;
026:
027: import org.w3c.dom.Element;
028:
029: import com.nabhinc.util.XMLUtil;
030: import com.nabhinc.ws.core.PropertyInfo;
031: import com.nabhinc.ws.core.WebServiceException;
032: import com.nabhinc.ws.core.WebServiceUtil;
033:
034: /**
035: * This class maintains all configuration information related to an
036: * interceptor. In addition it takes care of managing interceptor life-cycle.
037: *
038: * @author Padmanabh Dabke
039: * (c) 2005 Nabh Information Systems, Inc. All Rights Reserved.
040: */
041: public class InterceptorInfo {
042:
043: // Configuration Attributes
044:
045: /**
046: * Interceptor name
047: */
048: public String name = null;
049:
050: /**
051: * Interceptor display name
052: */
053: public String displayName = null;
054:
055: /**
056: * Interceptor class
057: */
058: public String interceptorClass = null;
059:
060: /**
061: * Interceptor description
062: */
063: public String description = null;
064:
065: /**
066: * Interceptor properties
067: */
068: public PropertyInfo[] propertiesInfo = null;
069:
070: /**
071: * Flag indicating if the interceptor is critical to
072: * the Web service invocation. If an interceptor is marked
073: * critical, the service invocation will fail if the interceptor
074: * throws an exception.
075: */
076: public boolean isCritical = false;
077:
078: /**
079: * Flag indicating if this interceptor should be automatically
080: * loaded.
081: */
082: public boolean manualLoad = false;
083:
084: /**
085: * "Run-as" this user.
086: */
087: public String owner = null;
088:
089: // Computed attributes
090:
091: /**
092: * Server context
093: */
094: public WebServiceContext webServiceContext = null;
095:
096: /**
097: * Interceptor load status
098: */
099: public int status = WebServiceServerConstants.LOAD_STATUS_UNLOADED;
100:
101: /**
102: * Error encountered during interceptor loading/invocation
103: */
104: public Throwable loadError = null;
105:
106: /**
107: * Time at which the interceptor was loaded/started.
108: */
109: public long startTime = 0;
110:
111: /**
112: * Interceptor instance. Non-null if loaded.
113: */
114: public Interceptor interceptor = null;
115:
116: public void init(Element config, WebServiceContext wsContext)
117: throws WebServiceException {
118: webServiceContext = wsContext;
119: name = XMLUtil.getSubElementText(config, "name");
120: displayName = XMLUtil.getSubElementText(config, "display-name");
121: interceptorClass = XMLUtil.getSubElementText(config, "class");
122: description = XMLUtil.getSubElementText(config, "description");
123: owner = XMLUtil.getSubElementText(config, "owner");
124: try {
125: propertiesInfo = WebServiceUtil
126: .deserializePropertiesInfo(config);
127: } catch (Exception ex) {
128: throw new WebServiceException(
129: "Failed to deserialize web services properties.",
130: ex);
131: }
132: isCritical = XMLUtil.getSubElement(config, "critical") != null;
133: manualLoad = XMLUtil.getSubElement(config, "manual-load") != null;
134:
135: if (name == null)
136: throw new WebServiceException("Missing interceptor name.");
137: if (interceptorClass == null)
138: throw new WebServiceException(
139: "Missing class name for interceptor " + name + ".");
140: }
141:
142: /**
143: * Constructs an XML representation of the interceptor configuratin and
144: * writes it to the provided writer
145: * @param indent Base indent for formatting the XML
146: * @param delta Increment added to go to the next indent level
147: * @param w Writer to which the serialized config is written
148: * @throws IOException
149: */
150: public void serialize(String indent, String delta, Writer w)
151: throws IOException {
152: XMLUtil.writeElementStart(indent, "interceptor", w);
153: String indent1 = indent + delta;
154: XMLUtil.writeElement(indent1, "name", name, w);
155: XMLUtil.writeElement(indent1, "class", interceptorClass, w);
156: XMLUtil.writeElement(indent1, "display-name", displayName, w);
157: XMLUtil.writeElement(indent1, "description", description, w);
158: XMLUtil.writeElement(indent1, "owner", owner, w);
159: if (isCritical)
160: XMLUtil.writeEmptyElement(indent1, "critical", w);
161: if (manualLoad)
162: XMLUtil.writeEmptyElement(indent1, "manual-load", w);
163: WebServiceUtil.serializePropertiesInfo(propertiesInfo, indent1,
164: delta, w);
165: XMLUtil.writeElementEnd(indent, "interceptor", w);
166: }
167:
168: /**
169: * Stores the new interceptor properties. If the interceptor is running,
170: * it attempts to set the properties on the running instance.
171: * @param newProps New interceptor properties
172: * @throws ServletException
173: */
174: public void setPropertiesInfo(PropertyInfo[] newProps)
175: throws WebServiceException {
176: if (interceptor != null) {
177: interceptor.setProperties(newProps);
178: }
179: propertiesInfo = newProps;
180:
181: }
182:
183: /**
184: * Attempts to load the interceptor if it is not already loaded.
185: * @throws ServletException Interceptor loading exception
186: */
187: public void load() throws WebServiceException {
188:
189: // If interceptor is not null, assume that it is successfully
190: // loaded. Just return;
191: if (interceptor != null)
192: return;
193:
194: try {
195: // Set the status to error here. If things go smoothly, it will be
196: // set to loaded at the end of the try block.
197: status = WebServiceServerConstants.LOAD_STATUS_INIT_ERROR;
198: Interceptor intc = (Interceptor) Class.forName(
199: interceptorClass).newInstance();
200: intc.init(new ServerObjectConfigImpl(webServiceContext,
201: propertiesInfo));
202: interceptor = intc;
203: status = WebServiceServerConstants.LOAD_STATUS_LOADED;
204: loadError = null;
205: startTime = System.currentTimeMillis();
206: } catch (ClassNotFoundException ex) {
207: loadError = ex;
208: throw new WebServiceException("Invalid class "
209: + interceptorClass + " for interceptor " + name
210: + ".", ex);
211: } catch (InstantiationException ex) {
212: loadError = ex;
213: throw new WebServiceException(
214: "Instantiation error in creating instance of class "
215: + interceptorClass + " for interceptor "
216: + name + ".", ex);
217: } catch (ClassCastException ex) {
218: loadError = ex;
219: throw new WebServiceException(
220: "Class must implement WebServiceInterceptor interface.",
221: ex);
222: } catch (IllegalAccessException ex) {
223: loadError = ex;
224: throw new WebServiceException(
225: "Illegal access error in creating instance of class "
226: + interceptorClass + " for Web service "
227: + name + ".", ex);
228: } catch (Exception ex) {
229: loadError = ex;
230: throw new WebServiceException(
231: "Failed to create interceptor " + name + ".", ex);
232: }
233: }
234:
235: /**
236: * Unload the interceptor and load again.
237: * @throws ServletException
238: */
239: public void reload() throws WebServiceException {
240: unload();
241: load();
242: }
243:
244: /**
245: * Destroys the interceptor if running and resets load state.
246: *
247: */
248: public void unload() {
249: if (interceptor != null) {
250: status = WebServiceServerConstants.LOAD_STATUS_UNLOADED;
251: interceptor.destroy();
252: interceptor = null;
253: }
254:
255: }
256:
257: }
|