View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.deploy;
20  
21  import org.eclipse.jetty.server.handler.ContextHandler;
22  import org.eclipse.jetty.util.AttributesMap;
23  
24  /**
25   * The information about an App that is managed by the {@link DeploymentManager}
26   */
27  public class App
28  {
29      private final DeploymentManager _manager;
30      private final AppProvider _provider;
31      private final String _originId;
32      private ContextHandler _context;
33  
34      /**
35       * Create an App with specified Origin ID and archivePath
36       * 
37       * @param originId
38       *            the origin ID (The ID that the {@link AppProvider} knows
39       *            about)
40       * @see App#getOriginId()
41       * @see App#getContextPath()
42       */
43      public App(DeploymentManager manager, AppProvider provider, String originId)
44      {
45          _manager = manager;
46          _provider = provider;
47          _originId = originId;
48      }
49  
50      /**
51       * Create an App with specified Origin ID and archivePath
52       * 
53       * @param originId
54       *            the origin ID (The ID that the {@link AppProvider} knows
55       *            about)
56       * @see App#getOriginId()
57       * @see App#getContextPath()
58       * @param context
59       *            Some implementations of AppProvider might have to use an
60       *            already created ContextHandler.
61       */
62      public App(DeploymentManager manager, AppProvider provider, String originId, ContextHandler context)
63      {
64          this(manager,provider,originId);
65          _context = context;
66      }
67  
68      /* ------------------------------------------------------------ */
69      /**
70       * @return The deployment manager
71       */
72      public DeploymentManager getDeploymentManager()
73      {
74          return _manager;
75      }
76  
77      /* ------------------------------------------------------------ */
78      /**
79       * @return The AppProvider
80       */
81      public AppProvider getAppProvider()
82      {
83          return _provider;
84      }
85  
86      /* ------------------------------------------------------------ */
87      /**
88       * Get ContextHandler for the App.
89       * 
90       * Create it if needed.
91       * 
92       * @return the {@link ContextHandler} to use for the App when fully started.
93       *         (Portions of which might be ignored when App is not yet 
94       *         {@link AppLifeCycle#DEPLOYED} or {@link AppLifeCycle#STARTED})
95       * @throws Exception
96       */
97      public ContextHandler getContextHandler() throws Exception
98      {
99          if (_context == null)
100         {
101             _context = getAppProvider().createContextHandler(this);
102             
103             AttributesMap attributes = _manager.getContextAttributes();
104             if (attributes!=null && attributes.size()>0)
105             {
106                 // Merge the manager attributes under the existing attributes
107                 attributes = new AttributesMap(attributes);
108                 attributes.addAll(_context.getAttributes());
109                 _context.setAttributes(attributes);
110             }
111         }
112         return _context;
113     }
114 
115     
116     /**
117      * The context path {@link App} relating to how it is installed on the
118      * jetty server side.
119      * 
120      * NOTE that although the method name indicates that this is a unique
121      * identifier, it is not, as many contexts may have the same contextPath,
122      * yet different virtual hosts.
123      * 
124      * @deprecated Use getContextPath instead.
125      * @return the context path for the App
126      */
127     public String getContextId()
128     {
129         return getContextPath();
130     }
131     
132     
133     /**
134      * The context path {@link App} relating to how it is installed on the
135      * jetty server side.
136      * 
137      * @return the contextPath for the App
138      */
139     public String getContextPath()
140     {
141         if (this._context == null)
142         {
143             return null;
144         }
145         return this._context.getContextPath();
146     }
147 
148 
149     /**
150      * The origin of this {@link App} as specified by the {@link AppProvider}
151      * 
152      * @return String representing the origin of this app.
153      */
154     public String getOriginId()
155     {
156         return this._originId;
157     }
158 
159     @Override
160     public String toString()
161     {
162         return "App[" + _context + "," + _originId + "]";
163     }
164 }