1 //
2 // ========================================================================
3 // Copyright (c) 1995-2013 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 * Get ContextHandler for the App.
88 *
89 * Create it if needed.
90 *
91 * @return the {@link ContextHandler} to use for the App when fully started.
92 * (Portions of which might be ignored when App is not yet
93 * {@link AppLifeCycle#DEPLOYED} or {@link AppLifeCycle#STARTED})
94 * @throws Exception
95 */
96 public ContextHandler getContextHandler() throws Exception
97 {
98 if (_context == null)
99 {
100 _context = getAppProvider().createContextHandler(this);
101
102 AttributesMap attributes = _manager.getContextAttributes();
103 if (attributes!=null && attributes.size()>0)
104 {
105 // Merge the manager attributes under the existing attributes
106 attributes = new AttributesMap(attributes);
107 attributes.addAll(_context.getAttributes());
108 _context.setAttributes(attributes);
109 }
110 }
111 return _context;
112 }
113
114
115 /**
116 * The context path {@link App} relating to how it is installed on the
117 * jetty server side.
118 *
119 * NOTE that although the method name indicates that this is a unique
120 * identifier, it is not, as many contexts may have the same contextPath,
121 * yet different virtual hosts.
122 *
123 * @deprecated Use getContextPath instead.
124 * @return the context path for the App
125 */
126 public String getContextId()
127 {
128 return getContextPath();
129 }
130
131
132 /**
133 * The context path {@link App} relating to how it is installed on the
134 * jetty server side.
135 *
136 * @return the contextPath for the App
137 */
138 public String getContextPath()
139 {
140 if (this._context == null)
141 {
142 return null;
143 }
144 return this._context.getContextPath();
145 }
146
147
148 /**
149 * The origin of this {@link App} as specified by the {@link AppProvider}
150 *
151 * @return String representing the origin of this app.
152 */
153 public String getOriginId()
154 {
155 return this._originId;
156 }
157
158 @Override
159 public String toString()
160 {
161 return "App[" + _context + "," + _originId + "]";
162 }
163 }