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:
020: package org.apache.synapse.handler;
021:
022: import org.apache.axis2.modules.Module;
023: import org.apache.axis2.context.ConfigurationContext;
024: import org.apache.axis2.description.AxisModule;
025: import org.apache.axis2.description.AxisDescription;
026: import org.apache.axis2.description.Parameter;
027: import org.apache.axis2.AxisFault;
028: import org.apache.neethi.Assertion;
029: import org.apache.neethi.Policy;
030: import org.apache.synapse.SynapseConstants;
031: import org.apache.synapse.SynapseException;
032: import org.apache.synapse.config.SynapseConfiguration;
033: import org.apache.synapse.core.axis2.SynapseInitializationModule;
034: import org.apache.synapse.core.SynapseEnvironment;
035: import org.apache.commons.logging.Log;
036: import org.apache.commons.logging.LogFactory;
037:
038: import java.io.File;
039:
040: /**
041: * This will be the Module class for the Synapse handler based mediations inside axis2 server. This
042: * will just set the default system property of SYNAPSE_XML to the repository/conf/synapse.xml in
043: * the axis2 servers repository and call the normal SynapseInitializationModule.init()
044: *
045: * @see org.apache.synapse.core.axis2.SynapseInitializationModule
046: */
047: public class SynapseModule implements Module {
048:
049: /**
050: * Log variable to be used as the logging appender
051: */
052: private static final Log log = LogFactory
053: .getLog(SynapseModule.class);
054:
055: /**
056: * Normal SynapseInitializationModule which initiates the Synapse
057: */
058: private SynapseInitializationModule initializationModule = null;
059:
060: /**
061: * This method will call the normal initiation after setting the SYNAPSE_XML file to get from
062: * the axis2 respository/conf folder
063: *
064: * @param configurationContext - ConfigurationContext of the Axis2 env
065: * @param axisModule - AxisModule describing handler initializationModule of Synapse
066: * @throws AxisFault - incase of a failure in initiation
067: */
068: public void init(ConfigurationContext configurationContext,
069: AxisModule axisModule) throws AxisFault {
070: if (System.getProperty(SynapseConstants.SYNAPSE_XML) == null) {
071: System.setProperty(SynapseConstants.SYNAPSE_XML,
072: configurationContext.getAxisConfiguration()
073: .getRepository().getPath()
074: + "/conf/synapse.xml");
075: }
076: if (new File(System.getProperty(SynapseConstants.SYNAPSE_XML))
077: .exists()) {
078: initializationModule = new org.apache.synapse.core.axis2.SynapseInitializationModule();
079: initializationModule.init(configurationContext, axisModule);
080:
081: // now initialize SynapseConfig
082: Parameter synEnv = configurationContext
083: .getAxisConfiguration().getParameter(
084: SynapseConstants.SYNAPSE_ENV);
085: Parameter synCfg = configurationContext
086: .getAxisConfiguration().getParameter(
087: SynapseConstants.SYNAPSE_CONFIG);
088: String message = "Unable to initialize the Synapse Configuration : Can not find the ";
089: if (synCfg == null
090: || synCfg.getValue() == null
091: || !(synCfg.getValue() instanceof SynapseConfiguration)) {
092: log.fatal(message + "Synapse Configuration");
093: throw new SynapseException(message
094: + "Synapse Configuration");
095: }
096:
097: if (synEnv == null
098: || synEnv.getValue() == null
099: || !(synEnv.getValue() instanceof SynapseEnvironment)) {
100: log.fatal(message + "Synapse Environment");
101: throw new SynapseException(message
102: + "Synapse Environment");
103: }
104:
105: ((SynapseConfiguration) synCfg.getValue())
106: .init((SynapseEnvironment) synEnv.getValue());
107: } else {
108: handleException("Unable to initialize the Synapse initializationModule. Couldn't "
109: + "find the configuration file in the location "
110: + System.getProperty(SynapseConstants.SYNAPSE_XML));
111: }
112: }
113:
114: /**
115: * Just do what the main SynapseInitializationModule tells you to do
116: *
117: * @param axisDescription
118: * @throws AxisFault
119: */
120: public void engageNotify(AxisDescription axisDescription)
121: throws AxisFault {
122: if (initializationModule != null) {
123: initializationModule.engageNotify(axisDescription);
124: } else {
125: handleException("Couldn't find the initializationModule");
126: }
127: }
128:
129: /**
130: * Just do what the main SynapseInitializationModule tells you to do
131: *
132: * @param assertion
133: * @return
134: */
135: public boolean canSupportAssertion(Assertion assertion) {
136: if (initializationModule != null) {
137: return initializationModule.canSupportAssertion(assertion);
138: } else {
139: return false;
140: }
141: }
142:
143: /**
144: * Just do what the main SynapseInitializationModule tells you to do
145: *
146: * @param policy
147: * @param axisDescription
148: * @throws AxisFault
149: */
150: public void applyPolicy(Policy policy,
151: AxisDescription axisDescription) throws AxisFault {
152: if (initializationModule != null) {
153: initializationModule.applyPolicy(policy, axisDescription);
154: } else {
155: handleException("Couldn't find the initializationModule");
156: }
157: }
158:
159: /**
160: * Just do what the main SynapseInitializationModule tells you to do
161: *
162: * @param configurationContext
163: * @throws AxisFault
164: */
165: public void shutdown(ConfigurationContext configurationContext)
166: throws AxisFault {
167: if (initializationModule != null) {
168: initializationModule.shutdown(configurationContext);
169: } else {
170: handleException("Couldn't find the initializationModule");
171: }
172: }
173:
174: private void handleException(String message) throws AxisFault {
175: log.error(message);
176: throw new AxisFault(message);
177: }
178: }
|