001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.axis2.scripting;
020:
021: import java.io.File;
022: import java.io.OutputStreamWriter;
023: import java.io.UnsupportedEncodingException;
024: import java.net.URISyntaxException;
025: import java.net.URL;
026: import java.net.URLDecoder;
027:
028: import org.apache.axis2.AxisFault;
029: import org.apache.axis2.context.ConfigurationContext;
030: import org.apache.axis2.deployment.DeploymentException;
031: import org.apache.axis2.description.AxisDescription;
032: import org.apache.axis2.description.AxisModule;
033: import org.apache.axis2.description.Parameter;
034: import org.apache.axis2.engine.AxisConfiguration;
035: import org.apache.axis2.modules.Module;
036: import org.apache.commons.logging.Log;
037: import org.apache.commons.logging.LogFactory;
038: import org.apache.neethi.Assertion;
039: import org.apache.neethi.Policy;
040:
041: /**
042: * Axis2 module to initialize script services. Uses a ScriptDeploymentEngine to
043: * find all the scripts in the scriptServices directory and deploy them as Axis2
044: * services.
045: */
046: public class ScriptModule implements Module {
047:
048: private static final Log log = LogFactory
049: .getLog(ScriptModule.class);
050:
051: static String defaultEncoding = new OutputStreamWriter(System.out)
052: .getEncoding();
053:
054: /**
055: * Init by creating and deploying AxisServices for each script
056: */
057: public void init(ConfigurationContext configContext,
058: AxisModule module) throws AxisFault {
059:
060: log.debug("script services init");
061:
062: AxisConfiguration axisConfig = configContext
063: .getAxisConfiguration();
064: if (axisConfig.getRepository() == null) {
065: log
066: .error("AxisConfiguration getRepository returns null, cannot deploy scripts");
067: } else {
068: File scriptServicesDirectory = getScriptServicesDirectory(axisConfig);
069: ScriptDeploymentEngine deploymentEngine = new ScriptDeploymentEngine(
070: axisConfig);
071: deploymentEngine.loadRepository(scriptServicesDirectory);
072: deploymentEngine.loadServices();
073: }
074: log.info("script module activated");
075: }
076:
077: /**
078: * Gets the repo directory for the scripts. The scripts directory is a
079: * sub-directory of the Axis2 repository directory. Its name may be defined
080: * by a 'scriptServicesDir' property in the axis2.xml otherwise it defaults
081: * a to a directory named 'scriptServices'.
082: */
083: protected File getScriptServicesDirectory(
084: AxisConfiguration axisConfig) throws DeploymentException {
085: try {
086:
087: URL axis2Repository = axisConfig.getRepository();
088: Parameter scriptsDirParam = axisConfig
089: .getParameter("scriptServicesDir");
090: String scriptsDir = scriptsDirParam == null ? "scriptServices"
091: : (String) scriptsDirParam.getValue();
092:
093: String path = URLDecoder.decode(axis2Repository.getPath(),
094: defaultEncoding);
095: java.io.File repoDir = new java.io.File(path.replace('/',
096: File.separatorChar).replace('|', ':'));
097:
098: return new File(repoDir, scriptsDir);
099: } catch (UnsupportedEncodingException e) {
100: throw new DeploymentException(
101: "UnsupportedEncodingException getting script service directory",
102: e);
103: }
104: }
105:
106: // --- the following are unused methods on the Module interface ---
107:
108: public void applyPolicy(Policy policy,
109: AxisDescription axisDescription) throws AxisFault {
110: }
111:
112: public boolean canSupportAssertion(Assertion assertion) {
113: return false;
114: }
115:
116: public void engageNotify(AxisDescription axisDescription)
117: throws AxisFault {
118: }
119:
120: public void shutdown(ConfigurationContext configurationContext)
121: throws AxisFault {
122: }
123:
124: }
|