001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.j2ee.deployserver;
031:
032: import com.caucho.config.Config;
033: import com.caucho.hessian.io.*;
034: import com.caucho.j2ee.deployclient.TargetImpl;
035: import com.caucho.j2ee.deployclient.TargetModuleIDImpl;
036: import com.caucho.log.Log;
037: import com.caucho.util.IntMap;
038: import com.caucho.util.L10N;
039:
040: import javax.enterprise.deploy.spi.TargetModuleID;
041: import javax.enterprise.deploy.spi.status.ProgressObject;
042: import javax.servlet.GenericServlet;
043: import javax.servlet.ServletException;
044: import javax.servlet.ServletRequest;
045: import javax.servlet.ServletResponse;
046: import java.io.IOException;
047: import java.io.InputStream;
048: import java.io.OutputStream;
049: import java.util.logging.Level;
050: import java.util.logging.Logger;
051:
052: /**
053: * Manager for the deployments.
054: */
055: public class DeploymentServlet extends GenericServlet {
056: private static final L10N L = new L10N(DeploymentServlet.class);
057: private static final Logger log = Logger
058: .getLogger(DeploymentServlet.class.getName());
059:
060: private static final int GET_TARGETS = 1;
061: private static final int DISTRIBUTE = 2;
062: private static final int GET_AVAILABLE_MODULES = 3;
063: private static final int UNDEPLOY = 4;
064: private static final int START = 5;
065: private static final int STOP = 6;
066:
067: private static final IntMap _methodMap = new IntMap();
068:
069: private DeploymentService _deploymentService;
070:
071: public void init() throws ServletException {
072: super .init();
073:
074: _deploymentService = DeploymentService.getDeploymentService();
075: }
076:
077: /**
078: * Serves the deployment.
079: */
080: public void service(ServletRequest req, ServletResponse res)
081: throws IOException, ServletException {
082: InputStream is = req.getInputStream();
083: OutputStream os = res.getOutputStream();
084:
085: Hessian2Input in = new Hessian2Input(is);
086: HessianOutput out = new HessianOutput(os);
087:
088: in.readCall();
089: String method = in.readMethod();
090:
091: try {
092: switch (_methodMap.get(method)) {
093: case GET_TARGETS:
094: in.completeCall();
095: out.startReply();
096: out.writeObject(_deploymentService.getTargets());
097: out.completeReply();
098: break;
099:
100: case GET_AVAILABLE_MODULES: {
101: String type = in.readString();
102: in.completeCall();
103:
104: out.startReply();
105: out.writeObject(_deploymentService
106: .getAvailableModules(type));
107: out.completeReply();
108: break;
109: }
110:
111: case DISTRIBUTE: {
112: TargetImpl[] targets = (TargetImpl[]) in
113: .readObject(TargetImpl[].class);
114: DeploymentPlan plan = new DeploymentPlan();
115:
116: InputStream planIs = in.readInputStream();
117:
118: try {
119: new Config().configure(plan, planIs);
120: } finally {
121: planIs.close();
122: }
123:
124: InputStream archiveIs = in.readInputStream();
125:
126: ProgressObject po = _deploymentService.distribute(
127: targets, archiveIs, plan);
128:
129: // use up all of the input or hessian throws
130: // an execption and hides error reported in the progress object
131: try {
132: while (archiveIs.read() != -1) {
133: }
134: } catch (Exception t) {
135: if (log.isLoggable(Level.FINEST))
136: log.log(Level.FINEST, t.toString(), t);
137: }
138:
139: in.completeCall();
140:
141: if (log.isLoggable(Level.FINEST))
142: log.log(Level.FINEST, String.valueOf(po));
143:
144: out.startReply();
145: out.writeObject(po);
146: out.completeReply();
147: break;
148: }
149:
150: case UNDEPLOY: {
151: TargetModuleID[] targetIDs;
152: targetIDs = (TargetModuleID[]) in
153: .readObject(TargetModuleID[].class);
154:
155: ProgressObject po = _deploymentService
156: .undeploy(targetIDs);
157:
158: in.completeCall();
159: out.startReply();
160: out.writeObject(po);
161: out.completeReply();
162: break;
163: }
164:
165: case START: {
166: TargetModuleID[] targetModuleIDs;
167: targetModuleIDs = (TargetModuleID[]) in
168: .readObject(TargetModuleIDImpl[].class);
169:
170: ProgressObject po = _deploymentService
171: .start(targetModuleIDs);
172:
173: in.completeCall();
174: out.startReply();
175: out.writeObject(po);
176: out.completeReply();
177: break;
178: }
179:
180: case STOP: {
181: TargetModuleID[] targetModuleIDs;
182: targetModuleIDs = (TargetModuleID[]) in
183: .readObject(TargetModuleIDImpl[].class);
184:
185: ProgressObject po = _deploymentService
186: .stop(targetModuleIDs);
187:
188: in.completeCall();
189: out.startReply();
190: out.writeObject(po);
191: out.completeReply();
192: break;
193: }
194:
195: default:
196: out.startReply();
197: out.writeFault("UnknownMethod", "UnknownMethod: "
198: + method, null);
199: out.completeReply();
200: break;
201: }
202: } catch (Exception e) {
203: log.log(Level.FINE, e.toString(), e);
204:
205: out.startReply();
206: out.writeFault(e.toString(), e.toString(), e);
207: out.completeReply();
208: }
209: }
210:
211: static {
212: _methodMap.put("getTargets", GET_TARGETS);
213: _methodMap.put("distribute", DISTRIBUTE);
214: _methodMap.put("getAvailableModules", GET_AVAILABLE_MODULES);
215: _methodMap.put("undeploy", UNDEPLOY);
216: _methodMap.put("start", START);
217: _methodMap.put("stop", STOP);
218: }
219: }
|