1
+ /*
2
+ * JBoss, Home of Professional Open Source
3
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
4
+ * by the @authors tag. See the copyright.txt in the distribution for a
5
+ * full listing of individual contributors.
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+ package org .jboss .as .arquillian .container ;
18
+
19
+ import java .io .ByteArrayInputStream ;
20
+ import java .io .InputStream ;
21
+ import java .util .HashMap ;
22
+ import java .util .Map ;
23
+ import java .util .concurrent .Future ;
24
+ import java .util .logging .Logger ;
25
+
26
+ import javax .management .MalformedObjectNameException ;
27
+ import javax .management .ObjectName ;
28
+
29
+ import org .jboss .arquillian .spi .client .container .DeployableContainer ;
30
+ import org .jboss .arquillian .spi .client .container .DeploymentException ;
31
+ import org .jboss .arquillian .spi .client .protocol .ProtocolDescription ;
32
+ import org .jboss .arquillian .spi .client .protocol .metadata .HTTPContext ;
33
+ import org .jboss .arquillian .spi .client .protocol .metadata .ProtocolMetaData ;
34
+ import org .jboss .arquillian .spi .client .protocol .metadata .Servlet ;
35
+ import org .jboss .as .controller .client .ModelControllerClient ;
36
+ import org .jboss .as .controller .client .helpers .standalone .DeploymentAction ;
37
+ import org .jboss .as .controller .client .helpers .standalone .DeploymentPlan ;
38
+ import org .jboss .as .controller .client .helpers .standalone .DeploymentPlanBuilder ;
39
+ import org .jboss .as .controller .client .helpers .standalone .ServerDeploymentActionResult ;
40
+ import org .jboss .as .controller .client .helpers .standalone .ServerDeploymentManager ;
41
+ import org .jboss .as .controller .client .helpers .standalone .ServerDeploymentPlanResult ;
42
+ import org .jboss .modules .management .ObjectProperties ;
43
+ import org .jboss .shrinkwrap .api .Archive ;
44
+ import org .jboss .shrinkwrap .api .exporter .ZipExporter ;
45
+ import org .jboss .shrinkwrap .descriptor .api .Descriptor ;
46
+
47
+ /**
48
+ * A JBossAS server connector
49
+ *
50
+
51
+ * @since 17-Nov-2010
52
+ */
53
+ public abstract class AbstractDeployableContainer <T extends JBossAsCommonConfiguration > implements DeployableContainer <T > {
54
+
55
+ protected static final ObjectName OBJECT_NAME ;
56
+
57
+ static {
58
+ try {
59
+ OBJECT_NAME = new ObjectName ("jboss.msc" , ObjectProperties .properties (
60
+ ObjectProperties .property ("type" , "container" ), ObjectProperties .property ("name" , "jboss-as" )));
61
+ } catch (MalformedObjectNameException e ) {
62
+ throw new IllegalStateException (e );
63
+ }
64
+ }
65
+
66
+ private static final Logger log = Logger .getLogger (AbstractDeployableContainer .class .getName ());
67
+
68
+ private T containerConfig ;
69
+ private ServerDeploymentManager deploymentManager ;
70
+
71
+ private final Map <Object , String > registry = new HashMap <Object , String >();
72
+
73
+ @ Override
74
+ public ProtocolDescription getDefaultProtocol () {
75
+ return new ProtocolDescription ("Servlet 3.0" );
76
+ }
77
+
78
+ @ Override
79
+ public void setup (T configuration ) {
80
+ containerConfig = configuration ;
81
+ ModelControllerClient client = ModelControllerClient .Factory .create (containerConfig .getBindAddress (),
82
+ containerConfig .getManagementPort ());
83
+
84
+ deploymentManager = ServerDeploymentManager .Factory .create (client );
85
+ }
86
+
87
+ @ Override
88
+ public void deploy (Descriptor descriptor ) throws DeploymentException {
89
+ deploy (descriptor .getDescriptorName (), descriptor , new ByteArrayInputStream (descriptor .exportAsString ().getBytes ()));
90
+ }
91
+
92
+ @ Override
93
+ public void undeploy (Descriptor descriptor ) throws DeploymentException {
94
+ undeploy ((Object ) descriptor );
95
+ }
96
+
97
+ @ Override
98
+ public ProtocolMetaData deploy (Archive <?> archive ) throws DeploymentException {
99
+ String uniqueDeploymentName = deploy (archive .getName (), archive , archive .as (ZipExporter .class ).exportAsInputStream ());
100
+
101
+ return getProtocolMetaData (archive , uniqueDeploymentName );
102
+ }
103
+
104
+ @ Override
105
+ public void undeploy (Archive <?> archive ) throws DeploymentException {
106
+ undeploy ((Object ) archive );
107
+ }
108
+
109
+ // TODO: can't be done in a proper way, hack until Management API support Deployment Metadata
110
+ // protected abstract ProtocolMetaData getProtocolMetaData(String uniqueDeploymentName);
111
+
112
+ protected ProtocolMetaData getProtocolMetaData (Archive <?> archive , String uniqueDeploymentName ) {
113
+ ProtocolMetaData protocol = new ProtocolMetaData ().addContext (new HTTPContext (containerConfig .getBindAddress ()
114
+ .getHostAddress (), containerConfig .getHttpPort ()).add (new Servlet ("ArquillianServletRunner" ,
115
+ getContextRootName (archive ))));
116
+
117
+ return protocol ;
118
+ }
119
+
120
+ protected T getContainerConfiguration () {
121
+ return containerConfig ;
122
+ }
123
+
124
+ private String getContextRootName (Archive <?> archive ) {
125
+ String archiveName = archive .getName ();
126
+ if (archiveName .indexOf ('.' ) != -1 ) {
127
+ return archiveName .substring (0 , archiveName .indexOf ('.' ));
128
+ }
129
+ return archiveName ;
130
+ }
131
+
132
+ private void undeploy (Object deployment ) throws DeploymentException {
133
+ String runtimeName = registry .remove (deployment );
134
+ if (runtimeName != null ) {
135
+ try {
136
+ DeploymentPlanBuilder builder = deploymentManager .newDeploymentPlan ().withRollback ();
137
+ DeploymentPlan plan = builder .undeploy (runtimeName ).remove (runtimeName ).build ();
138
+ Future <ServerDeploymentPlanResult > future = deploymentManager .execute (plan );
139
+ future .get ();
140
+ } catch (Exception ex ) {
141
+ log .warning ("Cannot undeploy: " + runtimeName + ":" + ex .getMessage ());
142
+ }
143
+ }
144
+ }
145
+
146
+ private String deploy (String deploymentName , Object deployment , InputStream content ) throws DeploymentException {
147
+ try {
148
+ DeploymentPlanBuilder builder = deploymentManager .newDeploymentPlan ().withRollback ();
149
+ builder = builder .add (deploymentName , content ).andDeploy ();
150
+ DeploymentPlan plan = builder .build ();
151
+ DeploymentAction deployAction = builder .getLastAction ();
152
+
153
+ return executeDeploymentPlan (plan , deployAction , deployment );
154
+
155
+ } catch (Exception e ) {
156
+ throw new DeploymentException ("Could not deploy to container" , e );
157
+ }
158
+ }
159
+
160
+ private String executeDeploymentPlan (DeploymentPlan plan , DeploymentAction deployAction , Object deployment )
161
+ throws Exception {
162
+ Future <ServerDeploymentPlanResult > future = deploymentManager .execute (plan );
163
+ registry .put (deployment , deployAction .getDeploymentUnitUniqueName ());
164
+ ServerDeploymentPlanResult planResult = future .get ();
165
+
166
+ ServerDeploymentActionResult actionResult = planResult .getDeploymentActionResult (deployAction .getId ());
167
+ if (actionResult != null ) {
168
+ Exception deploymentException = (Exception ) actionResult .getDeploymentException ();
169
+ if (deploymentException != null )
170
+ throw deploymentException ;
171
+ }
172
+ return deployAction .getDeploymentUnitUniqueName ();
173
+ }
174
+ }
0 commit comments