Skip to content

Package: GlassfishContainer$Monitor

GlassfishContainer$Monitor

nameinstructionbranchcomplexitylinemethod
handleProgressEvent(ProgressEvent)
M: 15 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
join(ProgressObject, String)
M: 12 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
join(String)
M: 20 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Distribution License v. 1.0, which is available at
6: * http://www.eclipse.org/org/documents/edl-v10.php.
7: *
8: * SPDX-License-Identifier: BSD-3-Clause
9: */
10:
11: package com.sun.xml.ws.test.container.gf;
12:
13: //import com.sun.enterprise.deployapi.SunDeploymentFactory;
14: import com.sun.istack.NotNull;
15: import com.sun.xml.ws.test.container.AbstractApplicationContainer;
16: import com.sun.xml.ws.test.container.Application;
17: import com.sun.xml.ws.test.container.ApplicationContainer;
18: import com.sun.xml.ws.test.container.DeployedService;
19: import com.sun.xml.ws.test.tool.WsTool;
20:
21: import javax.enterprise.deploy.spi.DeploymentManager;
22: import javax.enterprise.deploy.spi.Target;
23: import javax.enterprise.deploy.spi.TargetModuleID;
24: import javax.enterprise.deploy.spi.status.DeploymentStatus;
25: import javax.enterprise.deploy.spi.status.ProgressEvent;
26: import javax.enterprise.deploy.spi.status.ProgressListener;
27: import javax.enterprise.deploy.spi.status.ProgressObject;
28: import java.io.File;
29: import java.net.URL;
30:
31: /**
32: * {@link ApplicationContainer} implementation for Glassfish.
33: *
34: * Connection is made via JMX, so this works with GF running anywhere.
35: *
36: * @author Kohsuke Kawaguchi
37: */
38: public final class GlassfishContainer extends AbstractApplicationContainer {
39:
40: private final DeploymentManager dm;
41: private final Target[] targets;
42:
43: /**
44: * HTTP address of the remote server where we access the application.
45: */
46: private final URL httpServerUrl;
47:
48: /**
49: *
50: * @param httpServerUrl
51: * URL of the HTTP server. This is where we access deployed applications.
52: * @param host
53: * The host name of the JMX connection.
54: * @param port
55: * The administration TCP port. Usually 4848.
56: * @param userName
57: * Admin user name. Needed to connect to the admin port.
58: * @param password
59: * Admin user password.
60: */
61: public GlassfishContainer(WsTool wsimport, WsTool wsgen, URL httpServerUrl, String host, int port, String userName, String password, boolean httpspi) throws Exception {
62: super(wsimport, wsgen, httpspi);
63:
64: this.httpServerUrl = httpServerUrl;
65:
66: System.out.println("Connecting to Glassfish");
67:
68: String connectionUri = "deployer:Sun:AppServer::"+host+":"+port;
69: // to be more correct, we should load this from manifest.
70: // but that requires a local glassfish installation
71: dm = null; //new SunDeploymentFactory().getDeploymentManager(connectionUri,userName,password);
72: if (dm == null)
73: throw new UnsupportedOperationException("TODO: FIX ME!!!");
74:
75: targets = dm.getTargets();
76: if (targets.length == 0)
77: throw new Exception("Can't find deployment targets for Glassfish");
78: }
79:
80:
81: public String getTransport() {
82: return "http";
83: }
84:
85: public void start() throws Exception {
86: // noop. assumed to be running
87: }
88:
89: public void shutdown() throws Exception {
90: // noop. assumed to be running
91: }
92:
93: @NotNull
94: public Application deploy(DeployedService service) throws Exception {
95: String contextPath = service.service.getGlobalUniqueName();
96: File archive = new File(service.workDir,contextPath+".war");
97:
98: createWARZip(service,archive);
99:
100: URL warURL = new URL(httpServerUrl, "/" + contextPath + "/");
101: return new GlassfishApplication( warURL, service,this,deploy(archive,warURL));
102: }
103:
104: /**
105: * Deploys an application and returns the list of deployed module(s).
106: */
107: private TargetModuleID[] deploy(File war, URL targetUrl) throws Exception {
108: System.out.println("Deploying a service to "+targetUrl);
109:
110: ProgressObject dpo = Monitor.join(
111: dm.distribute(targets, war, null),"deployment failed");
112:
113: TargetModuleID[] modules = dpo.getResultTargetModuleIDs();
114:
115: Monitor.join(dm.start(modules),"failed to start services");
116:
117: return modules;
118: }
119:
120: void undeploy(TargetModuleID[] modules, URL warURL) throws Exception {
121: System.out.println("Undeploying a service from "+warURL);
122: Monitor.join(dm.undeploy(modules),"undeploy operation failed");
123: }
124:
125:
126: /**
127: * Monitors the asynchronous progress of the JSR-88 operation.
128: */
129: private static final class Monitor implements ProgressListener {
130:
131: public static ProgressObject join(ProgressObject po, String errorMessage) throws Exception {
132: Monitor m = new Monitor();
133: po.addProgressListener(m);
134: m.join(errorMessage);
135: return po;
136: }
137:
138: private DeploymentStatus completionEvent;
139:
140: public synchronized void handleProgressEvent(ProgressEvent event) {
141: DeploymentStatus s = event.getDeploymentStatus();
142:• if(s.isFailed() || s.isCompleted()) {
143: completionEvent = s;
144: notifyAll();
145: }
146: }
147:
148: /**
149: * Wait till the asynchronous operation completes.
150: */
151: public synchronized void join(String errorMessage) throws Exception {
152:• while(completionEvent==null)
153: wait();
154:• if(completionEvent.isFailed())
155: throw new Exception(errorMessage+" : "+completionEvent.getMessage());
156: }
157: }
158: }