001: /*
002: * Copyright ? 2006 Sun Microsystems, Inc. All rights reserved.
003: *
004: * Sun Microsystems, Inc. has intellectual property rights relating to
005: * technology embodied in the product that is described in this document.
006: * In particular, and without limitation, these intellectual property
007: * rights may include one or more of the U.S. patents listed at
008: * http://www.sun.com/patents and one or more additional patents or
009: * pending patent applications in the U.S. and in other countries.
010: *
011: * U.S. Government Rights - Commercial software. Government users are subject
012: * to the Sun Microsystems, Inc. standard license agreement and applicable
013: * provisions of the FAR and its supplements. Use is subject to license terms.
014: * This distribution may include materials developed by third parties.
015: * Sun, Sun Microsystems, the Sun logo and Java are trademarks or registered
016: * trademarks of Sun Microsystems, Inc. in the U.S. and other countries.
017: */
018: package com.sun.portal.app.blog.env;
019:
020: import com.sun.portal.app.blog.BlogPortletException;
021: import com.sun.portal.app.blog.password.Password;
022: import java.io.IOException;
023: import java.io.InputStream;
024: import java.net.MalformedURLException;
025: import java.net.URL;
026: import javax.crypto.SecretKey;
027: import javax.servlet.ServletContextEvent;
028: import javax.servlet.ServletContextListener;
029:
030: /**
031: * Abstract base class for preferences.
032: *
033: * Implementations of this class define the concept of
034: * preferences for different runtime application runtime environments.
035: */
036: public abstract class AbstractPrefs implements Prefs,
037: ServletContextListener {
038: private static AbstractPrefs prefs;
039: private static Password password = new Password();
040:
041: private SecretKey secret;
042:
043: public AbstractPrefs() {
044: InputStream is = this .getClass().getClassLoader()
045: .getResourceAsStream("passwords.key");
046: try {
047: secret = password.readKey(is);
048: } catch (BlogPortletException bpe) {
049: throw new RuntimeException(bpe);
050: }
051: }
052:
053: public static AbstractPrefs getInstance() {
054: if (prefs == null) {
055: throw new IllegalStateException("prefs instance is null");
056: }
057:
058: return prefs;
059: }
060:
061: public static void setInstance(AbstractPrefs prefs) {
062: AbstractPrefs.prefs = prefs;
063: }
064:
065: public void contextInitialized(ServletContextEvent event) {
066: setInstance(this );
067: }
068:
069: public void contextDestroyed(ServletContextEvent event) {
070: //nothing
071: }
072:
073: public String getAppUserPassword() throws BlogPortletException {
074: if (isEncryptedPasswords()) {
075: return decrypt(getRawAppUserPassword());
076: } else {
077: return getRawAppUserPassword();
078: }
079: }
080:
081: public String getAappUserPassword() throws BlogPortletException {
082: if (isEncryptedPasswords()) {
083: return decrypt(getRawAappUserPassword());
084: } else {
085: return getRawAappUserPassword();
086: }
087: }
088:
089: public void setAppUserPassword(String val)
090: throws BlogPortletException {
091: if (isEncryptedPasswords()) {
092: setRawAppUserPassword(encrypt(val));
093: } else {
094: setRawAppUserPassword(val);
095: }
096: }
097:
098: /**
099: * Decrypt a password string.
100: *
101: * This method is provided as a service for subclasses.
102: */
103: protected String decrypt(String epw) throws BlogPortletException {
104: return password.decrypt(epw, secret);
105: }
106:
107: /**
108: * Encrypt a password string.
109: *
110: * This method is provided as a service for subclasses.
111: */
112: protected String encrypt(String dpw) throws BlogPortletException {
113: return password.encrypt(dpw, secret);
114: }
115:
116: protected abstract String getPreference(String key)
117: throws BlogPortletException;
118:
119: protected abstract void setPreference(String key, String val)
120: throws BlogPortletException;
121:
122: public abstract void store() throws BlogPortletException;
123:
124: public String getAppEntriesUrl() throws BlogPortletException {
125: return getPreference(PortletPreferenceKeys.APP_ENTRIES_URL);
126: }
127:
128: public String getAppUrl() throws BlogPortletException {
129: return getPreference(PortletPreferenceKeys.APP_URL);
130: }
131:
132: public String getAppUserName() throws BlogPortletException {
133: return getPreference(PortletPreferenceKeys.APP_USER_NAME);
134: }
135:
136: public String getRawAppUserPassword() throws BlogPortletException {
137: return getPreference(PortletPreferenceKeys.APP_USER_PASSWORD);
138: }
139:
140: public String getAappUrl() throws BlogPortletException {
141: return getPreference(PortletPreferenceKeys.AAPP_URL);
142: }
143:
144: public String getAappUserName() throws BlogPortletException {
145: return getPreference(PortletPreferenceKeys.AAPP_USER_NAME);
146: }
147:
148: public String getRawAappUserPassword() throws BlogPortletException {
149: return getPreference(PortletPreferenceKeys.AAPP_USER_PASSWORD);
150: }
151:
152: public String getHandle() throws BlogPortletException {
153: return getPreference(PortletPreferenceKeys.HANDLE);
154: }
155:
156: public String getConfigMode() throws BlogPortletException {
157: return getPreference(PortletPreferenceKeys.CONFIG_MODE);
158: }
159:
160: public String getMemberPermission() throws BlogPortletException {
161: return getPreference(PortletPreferenceKeys.MEMBER_PERMISSION);
162: }
163:
164: public String getUserUrl() throws BlogPortletException {
165: return getPreference(PortletPreferenceKeys.USER_URL);
166: }
167:
168: public boolean isEncryptedPasswords() throws BlogPortletException {
169: return new Boolean(
170: getPreference(PortletPreferenceKeys.ENCRYPTED_PASSWORDS))
171: .booleanValue();
172: }
173:
174: public int getPageSize() throws BlogPortletException {
175: return Integer.valueOf(
176: getPreference(PortletPreferenceKeys.PAGE_SIZE))
177: .intValue();
178: }
179:
180: public URL getSearchUrl() throws BlogPortletException {
181: String u = getPreference(PortletPreferenceKeys.SEARCH_URL);
182: if (u == null || u.length() == 0) {
183: return null;
184: }
185:
186: try {
187: URL url = new URL(u);
188: return url;
189: } catch (MalformedURLException mue) {
190: throw new BlogPortletException(mue);
191: }
192: }
193:
194: public String getSearchDatabase() throws BlogPortletException {
195: String db = getPreference(PortletPreferenceKeys.SEARCH_DATABASE);
196: if (db == null || db.length() == 0) {
197: return null;
198: }
199:
200: return db;
201: }
202:
203: public int getRefreshInterval() throws BlogPortletException {
204: return Integer.valueOf(
205: getPreference(PortletPreferenceKeys.REFRESH_INTERVAL))
206: .intValue();
207: }
208:
209: public void setAppUrl(String appUrl) throws BlogPortletException {
210: setPreference(PortletPreferenceKeys.APP_URL, appUrl);
211: }
212:
213: public void setAppEntriesUrl(String appEntriesUrl)
214: throws BlogPortletException {
215: setPreference(PortletPreferenceKeys.APP_ENTRIES_URL,
216: appEntriesUrl);
217: }
218:
219: public void setAppUserName(String appUserName)
220: throws BlogPortletException {
221: setPreference(PortletPreferenceKeys.APP_USER_NAME, appUserName);
222: }
223:
224: public void setRawAppUserPassword(String appUserPassword)
225: throws BlogPortletException {
226: setPreference(PortletPreferenceKeys.APP_USER_PASSWORD,
227: appUserPassword);
228: }
229:
230: public void setAappUrl(String aappUrl) throws BlogPortletException {
231: setPreference(PortletPreferenceKeys.AAPP_URL, aappUrl);
232: }
233:
234: public void setAappUserName(String aappUserName)
235: throws BlogPortletException {
236: setPreference(PortletPreferenceKeys.AAPP_USER_NAME,
237: aappUserName);
238: }
239:
240: public void setHandle(String handle) throws BlogPortletException {
241: setPreference(PortletPreferenceKeys.HANDLE, handle);
242: }
243: }
|