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.wso2.esb.services;
020:
021: import org.apache.axiom.om.OMElement;
022: import org.apache.axiom.om.impl.builder.StAXOMBuilder;
023: import org.apache.axis2.AxisFault;
024: import org.apache.axis2.context.ConfigurationContext;
025: import org.apache.axis2.description.Parameter;
026: import org.apache.axis2.engine.AxisConfiguration;
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029: import org.apache.synapse.SynapseConstants;
030: import org.apache.synapse.SynapseException;
031: import org.apache.synapse.config.SynapseConfiguration;
032: import org.apache.synapse.config.xml.XMLConfigurationBuilder;
033: import org.apache.synapse.config.xml.XMLConfigurationSerializer;
034: import org.apache.synapse.core.axis2.Axis2SynapseEnvironment;
035: import org.apache.synapse.core.axis2.MessageContextCreatorForAxis2;
036: import org.apache.synapse.core.axis2.ProxyService;
037: import org.wso2.esb.util.XMLPrettyPrinter;
038:
039: import javax.xml.stream.XMLStreamException;
040: import java.io.*;
041: import java.util.Iterator;
042:
043: /**
044: * This is a POJO for the configuration admin service
045: */
046: public class ConfigAdmin extends AbstractESBAdmin {
047:
048: private static final Log log = LogFactory.getLog(ConfigAdmin.class);
049:
050: public String getConfiguration() throws AxisFault {
051: try {
052: ByteArrayOutputStream stream = new ByteArrayOutputStream();
053: XMLConfigurationSerializer.serializeConfiguration(
054: getSynapseConfiguration(), stream);
055: StAXOMBuilder builder = new StAXOMBuilder(
056: new ByteArrayInputStream(stream.toByteArray()));
057: stream.reset();
058: XMLPrettyPrinter.prettify(builder.getDocumentElement(),
059: stream);
060: return new String(stream.toByteArray());
061: } catch (XMLStreamException e) {
062: log.error("Error serializing the Synapse configuration");
063: throw AxisFault.makeFault(e);
064: } catch (Exception e) {
065: log.error("Error serializing the Synapse configuration");
066: throw AxisFault.makeFault(e);
067: }
068: }
069:
070: public void updateConfiguration(OMElement configElement)
071: throws AxisFault {
072: try {
073: log.info("Stopping Proxy services...");
074: AxisConfiguration axisCfg = getAxisConfig();
075: Iterator iter = getSynapseConfiguration()
076: .getProxyServices().iterator();
077: while (iter.hasNext()) {
078: ProxyService proxy = (ProxyService) iter.next();
079: try {
080: axisCfg.removeService(proxy.getName());
081: } catch (AxisFault e) {
082: log.error("Error removing Proxy service : "
083: + proxy.getName(), e);
084: throw e;
085: }
086: }
087:
088: ByteArrayOutputStream stream = new ByteArrayOutputStream();
089: configElement.getFirstElement().serialize(stream);
090: SynapseConfiguration synCfg = XMLConfigurationBuilder
091: .getConfiguration(new ByteArrayInputStream(stream
092: .toByteArray()));
093: synCfg.setPathToConfigFile(getSynapseConfiguration()
094: .getPathToConfigFile());
095: synCfg.setAxisConfiguration(axisCfg);
096: Parameter synapseCtxParam = new Parameter(
097: SynapseConstants.SYNAPSE_CONFIG, null);
098: synapseCtxParam.setValue(synCfg);
099: MessageContextCreatorForAxis2.setSynConfig(synCfg);
100:
101: //set up synapse env
102: ConfigurationContext configCtx = getConfigContext();
103: Parameter synapseEnvParam = new Parameter(
104: SynapseConstants.SYNAPSE_ENV, null);
105: Axis2SynapseEnvironment synEnv = new Axis2SynapseEnvironment(
106: configCtx, synCfg);
107: synapseEnvParam.setValue(synEnv);
108: MessageContextCreatorForAxis2.setSynEnv(synEnv);
109:
110: try {
111: axisCfg.addParameter(synapseCtxParam);
112: axisCfg.addParameter(synapseEnvParam);
113:
114: } catch (AxisFault e) {
115: String msg = "Could not set parameters '"
116: + SynapseConstants.SYNAPSE_CONFIG
117: + "' and/or '" + SynapseConstants.SYNAPSE_ENV
118: + "'to the Axis2 configuration : "
119: + e.getMessage();
120: handleFault(log, msg, e);
121: }
122: // redeploy proxy services
123: log.info("Re-deploying Proxy services...");
124: iter = synCfg.getProxyServices().iterator();
125: while (iter.hasNext()) {
126: ProxyService proxy = (ProxyService) iter.next();
127: proxy.buildAxisService(synCfg, axisCfg);
128: log.info("Deployed Proxy service : " + proxy.getName());
129: if (!proxy.isStartOnLoad()) {
130: proxy.stop(synCfg);
131: }
132: }
133: // re-init the synapse configuration
134: synCfg.init(synEnv);
135:
136: log.info("Configuration updated successfully...");
137:
138: } catch (SynapseException se) {
139: handleFault(log,
140: "Unable to update the Synapse configuration."
141: + se.getMessage()
142: + " Check log for more details", se);
143: } catch (XMLStreamException e) {
144: handleFault(log,
145: "Unable to update the Synapse configuration."
146: + e.getMessage()
147: + " Check log for more details", e);
148: }
149: }
150:
151: public boolean saveConfigurationToDisk() throws AxisFault {
152: log.info("Saving configuration..");
153:
154: SynapseConfiguration config = getSynapseConfiguration();
155: try {
156: FileOutputStream fos = new FileOutputStream(config
157: .getPathToConfigFile());
158: XMLConfigurationSerializer.serializeConfiguration(config,
159: fos);
160: try {
161: fos.close();
162: XMLPrettyPrinter.prettify(new File(config
163: .getPathToConfigFile()));
164: } catch (IOException e) {
165: // ignore prettyfy errors
166: }
167: log.info("Configuration saved to disk");
168: return true;
169: } catch (XMLStreamException e) {
170: handleFault(log, "Could not save changes to disk."
171: + e.getMessage() + " Check log for more details", e);
172: } catch (FileNotFoundException e) {
173: handleFault(
174: log,
175: "Could not locate the Synapse configuration file to save changes",
176: e);
177: }
178: return false;
179: }
180:
181: }
|