2011/08/05 - Jakarta Cactus has been retired.

For more information, please explore the Attic.

View Javadoc

1   /* 
2    * ========================================================================
3    * 
4    * Licensed to the Apache Software Foundation (ASF) under one or more
5    * contributor license agreements.  See the NOTICE file distributed with
6    * this work for additional information regarding copyright ownership.
7    * The ASF licenses this file to You under the Apache License, Version 2.0
8    * (the "License"); you may not use this file except in compliance with
9    * the License.  You may obtain a copy of the License at
10   * 
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * 
19   * ========================================================================
20   */
21  package org.apache.cactus.container;
22  
23  import java.io.File;
24  import java.util.Map;
25  
26  import org.codehaus.cargo.container.Container;
27  import org.codehaus.cargo.container.InstalledLocalContainer;
28  import org.codehaus.cargo.container.property.GeneralPropertySet;
29  import org.codehaus.cargo.util.log.Logger;
30  
31  /**
32   * Class that wraps around an implementation of the <code>Container</code>
33   * interface and delegates all calls to the wrapped instance.
34   * 
35   * @version $Id: ContainerWrapper.java 239130 2005-01-29 15:49:18Z vmassol $
36   */
37  public class ContainerWrapper
38  {
39     /**
40      * The default constructor.
41      */
42      public ContainerWrapper()
43      { }
44  
45      // Instance Variables ------------------------------------------------------
46  
47      /**
48       * The nested container.
49       */
50      private org.codehaus.cargo.container.Container container;
51  
52      // Constructors ------------------------------------------------------------
53  
54      /**
55       * Constructor.
56       * 
57       * @param theContainer The container to wrap
58       */
59      public ContainerWrapper(org.codehaus.cargo.container.Container theContainer)
60      {
61          if (theContainer == null)
62          {
63              throw new NullPointerException("'theContainer' must not be null");
64          }
65          this.container = theContainer;
66      }
67      
68      // AbstractContainer Implementation ----------------------------------------
69  
70      /**
71       * {@inheritDoc}
72       * @see Container#getName()
73       */
74      public String getName()
75      {
76          return container.getName();
77      }
78      
79      /**
80       * {@inheritDoc}
81       * @see Container#getTestContext()
82       */
83      public String getTestContext()
84      {
85          return ((InstalledLocalContainer) container).getConfiguration()
86              .getPropertyValue("testContext");
87      }    
88      
89      /**
90       * {@inheritDoc}
91       * @see Container#getStartUpWait()
92       */
93      public long getStartUpWait()
94      {
95          //We don't need startup wait, as we integrate with cargo, 
96          //and it will do the work for us :-)
97         return 0L;
98      }
99      
100     /**
101      * {@inheritDoc}
102      * @see Container#getPort()
103      */
104     public int getPort()
105     {
106         String port = ((InstalledLocalContainer) container).getConfiguration()
107             .getPropertyValue("cargo.servlet.port");
108         if (port != null)
109         {
110             return Integer.parseInt(port);
111         }
112         else
113         {
114             return 8080;
115         }
116     }
117 
118     /**
119      * {@inheritDoc}
120      * @see Container#getToDir()
121      */
122     public File getToDir()
123     {
124         String location = ((InstalledLocalContainer) container)
125             .getConfiguration().getPropertyValue("cactus.toDir");
126         File toDir;
127         if (location != null) 
128         {
129             toDir = new File(location);
130         }
131         else
132         {
133             toDir = new File("./target/" + container.getId() + "/");
134             toDir.mkdirs();
135         }
136         return toDir;
137     }
138 
139     /**
140      * {@inheritDoc}
141      * @see Container#init()
142      */
143     public void init()
144     {
145         //this.container.init();
146     }
147 
148     /**
149      * {@inheritDoc}
150      * @see Container#isEnabled()
151      */
152     public boolean isEnabled()
153     {
154         //For now containers are always enabled. Think of a way to implement.
155         return true;
156     }
157 
158     /**
159      * {@inheritDoc}
160      * @see Container#isExcluded(String)
161      */
162     public boolean isExcluded(String theTestName)
163     {
164         //No tests are excluded per container for now. Think of 
165         //a way to implement.
166         return false;
167     }
168 
169     /**
170      * {@inheritDoc}
171      * @see Container#startUp()
172      */
173     public void startUp()
174     {
175         ((InstalledLocalContainer) container).start();
176     }
177 
178     /**
179      * {@inheritDoc}
180      * @see Container#shutDown()
181      */
182     public void shutDown()
183     {
184         ((InstalledLocalContainer) container).stop();
185     }
186     
187     /**
188      * {@inheritDoc}
189      * @see Container#setSystemProperties
190      */
191     public void setSystemProperties(Map theProperties)
192     {
193         if (theProperties != null) 
194         {
195             ((InstalledLocalContainer) container).setSystemProperties(theProperties);
196         }
197     }
198 
199     /**
200      * {@inheritDoc}
201      * @see Container#setContainerClasspath(Path)
202      * @since Cactus 1.6
203      */
204     public void setContainerClasspath(String[] theClasspath)
205     {
206         if (theClasspath != null)
207         {
208             ((InstalledLocalContainer) container)
209                 .setExtraClasspath(theClasspath);
210         }
211     }
212 
213     /**
214      * {@inheritDoc}
215      * @see Container#getContainerClasspath()
216      * @since Cactus 1.6
217      */
218     public String[] getContainerClasspath()
219     {
220         return ((InstalledLocalContainer) container).getExtraClasspath();
221     }
222 
223     /**
224      * {@inheritDoc}
225      * @see Container#getServer()
226      */
227     public String getServer()
228     {
229         return ((InstalledLocalContainer) container).getConfiguration()
230             .getPropertyValue(GeneralPropertySet.HOSTNAME);
231     }
232 
233     /**
234      * {@inheritDoc}
235      * @see Container#getProtocol()
236      */
237     public String getProtocol()
238     {
239         return ((InstalledLocalContainer) container).getConfiguration()
240              .getPropertyValue(GeneralPropertySet.PROTOCOL);
241     }
242 
243     /**
244      * {@inheritDoc}
245      * @see Container#getBaseURL()
246      */
247     public String getBaseURL()
248     {
249         return getProtocol() + "://" + getServer() + ":" + getPort();
250     }
251     /**
252      * @param theLogger sets  the logger
253      */
254     public void setLogger(Logger theLogger)
255     {
256         this.container.setLogger(theLogger);
257     }
258     /**
259      *  @param theContainer - the container to set
260      */
261     public void setContainer(Container theContainer)
262     {
263         this.container = theContainer;
264     }
265 }