1 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 ; 42 import javax.enterprise.deploy.spi.status.ProgressObject ; 43 import javax.servlet.GenericServlet ; 44 import javax.servlet.ServletException ; 45 import javax.servlet.ServletRequest ; 46 import javax.servlet.ServletResponse ; 47 import java.io.IOException ; 48 import java.io.InputStream ; 49 import java.io.OutputStream ; 50 import java.util.logging.Level ; 51 import java.util.logging.Logger ; 52 53 56 public class DeploymentServlet 57 extends GenericServlet 58 { 59 private static final L10N L = new L10N(DeploymentServlet.class); 60 private static final Logger 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 75 { 76 super.init(); 77 78 _deploymentService = DeploymentService.getDeploymentService(); 79 } 80 81 84 public void service(ServletRequest req, ServletResponse res) 85 throws IOException , ServletException 86 { 87 InputStream is = req.getInputStream(); 88 OutputStream os = res.getOutputStream(); 89 90 HessianInput in = new HessianInput(is); 91 HessianOutput out = new HessianOutput(os); 92 93 in.readCall(); 94 String 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 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 planIs = in.readInputStream(); 122 123 try { 124 new Config().configure(plan, planIs); 125 } finally { 126 planIs.close(); 127 } 128 129 InputStream archiveIs = in.readInputStream(); 130 131 ProgressObject po = _deploymentService.distribute(targets, archiveIs, plan); 132 133 try { 136 while (archiveIs.read() != -1) {} 137 } 138 catch (Throwable 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 []targetIDs; 157 targetIDs = (TargetModuleID []) in.readObject(TargetModuleIDImpl[].class); 158 159 ProgressObject 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 []targetModuleIDs; 171 targetModuleIDs = (TargetModuleID []) in.readObject(TargetModuleIDImpl[].class); 172 173 ProgressObject 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 []targetModuleIDs; 185 targetModuleIDs = (TargetModuleID []) in.readObject(TargetModuleIDImpl[].class); 186 187 ProgressObject 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 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 |