KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > j2ee > deployserver > DeploymentServlet


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.j2ee.deployserver;
31
32 import com.caucho.config.Config;
33 import com.caucho.hessian.io.HessianInput;
34 import com.caucho.hessian.io.HessianOutput;
35 import com.caucho.j2ee.deployclient.TargetImpl;
36 import com.caucho.j2ee.deployclient.TargetModuleIDImpl;
37 import com.caucho.log.Log;
38 import com.caucho.util.IntMap;
39 import com.caucho.util.L10N;
40
41 import javax.enterprise.deploy.spi.TargetModuleID JavaDoc;
42 import javax.enterprise.deploy.spi.status.ProgressObject JavaDoc;
43 import javax.servlet.GenericServlet JavaDoc;
44 import javax.servlet.ServletException JavaDoc;
45 import javax.servlet.ServletRequest JavaDoc;
46 import javax.servlet.ServletResponse JavaDoc;
47 import java.io.IOException JavaDoc;
48 import java.io.InputStream JavaDoc;
49 import java.io.OutputStream JavaDoc;
50 import java.util.logging.Level JavaDoc;
51 import java.util.logging.Logger JavaDoc;
52
53 /**
54  * Manager for the deployments.
55  */

56 public class DeploymentServlet
57   extends GenericServlet JavaDoc
58 {
59   private static final L10N L = new L10N(DeploymentServlet.class);
60   private static final Logger JavaDoc log = Log.open(DeploymentServlet.class);
61
62   private static final int GET_TARGETS = 1;
63   private static final int DISTRIBUTE = 2;
64   private static final int GET_AVAILABLE_MODULES = 3;
65   private static final int UNDEPLOY = 4;
66   private static final int START = 5;
67   private static final int STOP = 6;
68
69   private static final IntMap _methodMap = new IntMap();
70
71   private DeploymentService _deploymentService;
72
73   public void init()
74     throws ServletException JavaDoc
75   {
76     super.init();
77
78     _deploymentService = DeploymentService.getDeploymentService();
79   }
80
81   /**
82    * Serves the deployment.
83    */

84   public void service(ServletRequest JavaDoc req, ServletResponse JavaDoc res)
85     throws IOException JavaDoc, ServletException JavaDoc
86   {
87     InputStream JavaDoc is = req.getInputStream();
88     OutputStream JavaDoc os = res.getOutputStream();
89
90     HessianInput in = new HessianInput(is);
91     HessianOutput out = new HessianOutput(os);
92
93     in.readCall();
94     String JavaDoc method = in.readMethod();
95
96     try {
97       switch (_methodMap.get(method)) {
98       case GET_TARGETS:
99         in.completeCall();
100     out.startReply();
101     out.writeObject(_deploymentService.getTargets());
102     out.completeReply();
103     break;
104
105       case GET_AVAILABLE_MODULES:
106     {
107       String JavaDoc type = in.readString();
108       in.completeCall();
109
110       out.startReply();
111       out.writeObject(_deploymentService.getAvailableModules(type));
112       out.completeReply();
113       break;
114     }
115
116       case DISTRIBUTE:
117     {
118       TargetImpl []targets = (TargetImpl[]) in.readObject(TargetImpl[].class);
119       DeploymentPlan plan = new DeploymentPlan();
120
121           InputStream JavaDoc planIs = in.readInputStream();
122
123           try {
124         new Config().configure(plan, planIs);
125       } finally {
126             planIs.close();
127       }
128
129       InputStream JavaDoc archiveIs = in.readInputStream();
130
131       ProgressObject JavaDoc po = _deploymentService.distribute(targets, archiveIs, plan);
132
133           // use up all of the input or hessian throws
134
// an execption and hides error reported in the progress object
135
try {
136             while (archiveIs.read() != -1) {}
137           }
138           catch (Throwable JavaDoc t) {
139             if (log.isLoggable(Level.FINEST))
140               log.log(Level.FINEST, t.toString(), t);
141           }
142
143           in.completeCall();
144
145           if (log.isLoggable(Level.FINEST))
146             log.log(Level.FINEST, String.valueOf(po));
147
148           out.startReply();
149           out.writeObject(po);
150       out.completeReply();
151       break;
152     }
153
154       case UNDEPLOY:
155     {
156       TargetModuleID JavaDoc []targetIDs;
157       targetIDs = (TargetModuleID JavaDoc []) in.readObject(TargetModuleIDImpl[].class);
158
159       ProgressObject JavaDoc po = _deploymentService.undeploy(targetIDs);
160
161       in.completeCall();
162       out.startReply();
163       out.writeObject(po);
164       out.completeReply();
165       break;
166     }
167
168         case START:
169         {
170           TargetModuleID JavaDoc []targetModuleIDs;
171           targetModuleIDs = (TargetModuleID JavaDoc []) in.readObject(TargetModuleIDImpl[].class);
172
173           ProgressObject JavaDoc po = _deploymentService.start(targetModuleIDs);
174
175           in.completeCall();
176           out.startReply();
177           out.writeObject(po);
178           out.completeReply();
179           break;
180         }
181
182         case STOP:
183         {
184           TargetModuleID JavaDoc []targetModuleIDs;
185           targetModuleIDs = (TargetModuleID JavaDoc []) in.readObject(TargetModuleIDImpl[].class);
186
187           ProgressObject JavaDoc po = _deploymentService.stop(targetModuleIDs);
188
189           in.completeCall();
190           out.startReply();
191           out.writeObject(po);
192           out.completeReply();
193           break;
194         }
195
196       default:
197     out.startReply();
198     out.writeFault("UnknownMethod", "UnknownMethod: " + method, null);
199     out.completeReply();
200     break;
201       }
202     } catch (Exception JavaDoc 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
212   static {
213     _methodMap.put("getTargets", GET_TARGETS);
214     _methodMap.put("distribute", DISTRIBUTE);
215     _methodMap.put("getAvailableModules", GET_AVAILABLE_MODULES);
216     _methodMap.put("undeploy", UNDEPLOY);
217     _methodMap.put("start", START);
218     _methodMap.put("stop", STOP);
219   }
220 }
221
222
Popular Tags