1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet;
24  
25  import com.liferay.portal.kernel.util.JavaConstants;
26  import com.liferay.portal.kernel.util.StringMaker;
27  import com.liferay.portal.model.PortletInfo;
28  import com.liferay.portal.model.impl.PortletImpl;
29  import com.liferay.portal.servlet.PortletContextPool;
30  import com.liferay.portal.servlet.PortletContextWrapper;
31  import com.liferay.portal.util.PortalInstances;
32  import com.liferay.util.CollectionFactory;
33  
34  import java.io.ByteArrayInputStream;
35  
36  import java.util.Collections;
37  import java.util.Enumeration;
38  import java.util.Locale;
39  import java.util.Map;
40  import java.util.PropertyResourceBundle;
41  import java.util.ResourceBundle;
42  
43  import javax.portlet.PortletConfig;
44  import javax.portlet.PortletContext;
45  
46  /**
47   * <a href="PortletConfigImpl.java.html"><b><i>View Source</i></b></a>
48   *
49   * @author Brian Wing Shun Chan
50   *
51   */
52  public class PortletConfigImpl implements PortletConfig {
53  
54      public PortletConfigImpl(String portletName, PortletContext portletCtx,
55                               Map params, String resourceBundle,
56                               PortletInfo portletInfo) {
57  
58          _rootPortletId = PortletImpl.getRootPortletId(portletName);
59          _portletId = portletName;
60          _portletName = _rootPortletId;
61  
62          int pos = _portletName.indexOf(PortletImpl.WAR_SEPARATOR);
63  
64          if (pos != -1) {
65              _portletName = _portletName.substring(0, pos);
66  
67              _warFile = true;
68          }
69  
70          _portletCtx = portletCtx;
71          _params = params;
72          _resourceBundle = resourceBundle;
73          _portletInfo = portletInfo;
74          _bundlePool = CollectionFactory.getHashMap();
75      }
76  
77      public String getPortletId() {
78          return _portletId;
79      }
80  
81      public String getPortletName() {
82          return _portletName;
83      }
84  
85      public boolean isWARFile() {
86          return _warFile;
87      }
88  
89      public PortletContext getPortletContext() {
90          return _portletCtx;
91      }
92  
93      public ResourceBundle getResourceBundle(Locale locale) {
94          if (_resourceBundle == null) {
95              String poolId = _portletId;
96  
97              ResourceBundle bundle = (ResourceBundle)_bundlePool.get(poolId);
98  
99              if (bundle == null) {
100                 StringMaker sm = new StringMaker();
101 
102                 try {
103                     sm.append(JavaConstants.JAVAX_PORTLET_TITLE);
104                     sm.append("=");
105                     sm.append(_portletInfo.getTitle());
106                     sm.append("\n");
107 
108                     sm.append(JavaConstants.JAVAX_PORTLET_SHORT_TITLE);
109                     sm.append("=");
110                     sm.append(_portletInfo.getShortTitle());
111                     sm.append("\n");
112 
113                     sm.append(JavaConstants.JAVAX_PORTLET_KEYWORDS);
114                     sm.append("=");
115                     sm.append(_portletInfo.getKeywords());
116                     sm.append("\n");
117 
118                     bundle = new PropertyResourceBundle(
119                         new ByteArrayInputStream(sm.toString().getBytes()));
120                 }
121                 catch (Exception e) {
122                     e.printStackTrace();
123                 }
124 
125                 _bundlePool.put(poolId, bundle);
126             }
127 
128             return bundle;
129         }
130         else {
131             String poolId = _portletId + "." + locale.toString();
132 
133             ResourceBundle bundle = (ResourceBundle)_bundlePool.get(poolId);
134 
135             if (bundle == null) {
136                 if (!_warFile &&
137                     _resourceBundle.equals(
138                         StrutsResourceBundle.class.getName())) {
139 
140                     long companyId = PortalInstances.getDefaultCompanyId();
141 
142                     bundle = StrutsResourceBundle.getBundle(
143                         _portletName, companyId, locale);
144                 }
145                 else {
146                     PortletContextWrapper pcw =
147                         PortletContextPool.get(_rootPortletId);
148 
149                     bundle = pcw.getResourceBundle(locale);
150                 }
151 
152                 bundle = new PortletResourceBundle(bundle, _portletInfo);
153 
154                 _bundlePool.put(poolId, bundle);
155             }
156 
157             return bundle;
158         }
159     }
160 
161     public String getInitParameter(String name) {
162         if (name == null) {
163             throw new IllegalArgumentException();
164         }
165 
166         return (String)_params.get(name);
167     }
168 
169     public Enumeration getInitParameterNames() {
170         return Collections.enumeration(_params.keySet());
171     }
172 
173     private String _rootPortletId;
174     private String _portletId;
175     private String _portletName;
176     private boolean _warFile;
177     private PortletContext _portletCtx;
178     private Map _params;
179     private String _resourceBundle;
180     private PortletInfo _portletInfo;
181     private Map _bundlePool;
182 
183 }