001: /*
002: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005: package com.sun.portal.desktop.context;
006:
007: import java.util.Properties;
008: import java.io.FileInputStream;
009: import java.io.File;
010: import java.io.IOException;
011: import java.io.BufferedReader;
012: import java.io.FileReader;
013: import java.io.FilenameFilter;
014: import java.io.BufferedWriter;
015: import java.io.FileWriter;
016: import java.io.StringReader;
017:
018: import java.util.Comparator;
019: import java.util.Locale;
020: import java.util.Set;
021: import java.util.HashSet;
022:
023: import javax.servlet.http.HttpServletRequest;
024:
025: public class PropertiesDPContext implements DPContext {
026: protected static final String ISVALIDATING_KEY = "isValidating";
027: protected static final String NAMESPACEURI_KEY = "nameSpaceURI";
028: protected static final String DOCUMENTPATH_KEY = "documentPath";
029: protected static final String DEFAULT_FILENAME = "/etc/opt/SUNWportal/dp-context.properties";
030:
031: protected static final String DEFAULT_DOCUMENT_FILENAME = "/etc/opt/SUNWportal/sampleportal/dp.xml";
032:
033: protected DesktopContext desktopContext = null;
034: protected Properties properties = null;
035: protected static FilenameFilter dpFilenameFilter = new DPDocumentFilenameFilter();
036: protected static DPFileComparator dpFileComparator = new DPFileComparator();
037:
038: public PropertiesDPContext() {
039: this (DEFAULT_FILENAME);
040: }
041:
042: public PropertiesDPContext(String filename) {
043: properties = new Properties();
044:
045: FileInputStream fis = null;
046: try {
047: fis = new FileInputStream(new File(filename));
048: properties.load(fis);
049: } catch (IOException ioe) {
050: throw new ContextError(
051: "PropertiesDPContext.PropertiesDPContext()", ioe);
052: }
053: }
054:
055: public void init(HttpServletRequest req) {
056: desktopContext = DesktopContextThreadLocalizer.get();
057: if (desktopContext == null) {
058: throw new ContextError(
059: "PropertiesDPContext.init(): desktop context was null");
060: }
061: }
062:
063: protected DesktopContext getDesktopContext() {
064: return desktopContext;
065: }
066:
067: public boolean isValidating() {
068: return new Boolean(properties.getProperty(ISVALIDATING_KEY))
069: .booleanValue();
070: }
071:
072: public String getNameSpaceURI() {
073: return properties.getProperty(NAMESPACEURI_KEY);
074: }
075:
076: protected String getDPDocumentPath() {
077: String filename = properties.getProperty(DOCUMENTPATH_KEY);
078:
079: return filename;
080: }
081:
082: public synchronized Locale getLocale() {
083: return getDesktopContext().getLocale();
084: }
085:
086: public Set getDPDocumentNames(String name) {
087: return getDPDocumentNames();
088: }
089:
090: public Set getDPDocumentNames() {
091: File docPath = new File(getDPDocumentPath());
092:
093: File[] dpDocuments = docPath.listFiles(dpFilenameFilter);
094:
095: Set names = new HashSet();
096:
097: for (int i = 0; i < dpDocuments.length; i++) {
098: File f = dpDocuments[i];
099: names.add(f.toString());
100: }
101:
102: return names;
103: }
104:
105: public String getDPDocument(String name) {
106: File f = new File(name);
107: return readFile(f).toString();
108: }
109:
110: public StringBuffer getDPUserDocument() {
111: String path = getDPDocumentPath();
112:
113: if (path == null) {
114: throw new ContextError(
115: "DPContext.getDPUserDocument(): path was null");
116: }
117:
118: File f = new File(path, "dp-user.xml");
119:
120: if (!f.exists()) {
121: return null;
122: }
123: if (!f.canRead()) {
124: return null;
125: }
126:
127: StringBuffer buf = readFile(f);
128:
129: return buf;
130: }
131:
132: public void storeDPUserDocument(String dp) {
133:
134: String path = getDPDocumentPath();
135: if (path == null) {
136: throw new ContextError("DPContext.getDPUserDocument(): "
137: + "path was null");
138: }
139: File f = new File(path, "dp-user.xml");
140: writeFile(f, dp);
141: }
142:
143: protected StringBuffer readFile(File f) {
144: if (!f.canRead()) {
145: throw new ContextError(
146: "PropertiesDPContext.readFile(): cannot read file: "
147: + f);
148: }
149:
150: StringBuffer buf = new StringBuffer();
151:
152: try {
153: BufferedReader br = new BufferedReader(new FileReader(f));
154:
155: String line = null;
156:
157: while ((line = br.readLine()) != null) {
158: buf.append(line);
159: }
160: br.close();
161: } catch (IOException ioe) {
162: throw new ContextError("PropertiesDPContext.readFile(): ",
163: ioe);
164: }
165:
166: return buf;
167: }
168:
169: protected void writeFile(File f, String s) {
170:
171: if (!f.exists()) {
172: try {
173: f.createNewFile();
174: } catch (IOException ioe) {
175: throw new ContextError(
176: "PropertiesDPContext.writeFile(): "
177: + "failed to create file: " + f, ioe);
178: }
179: }
180:
181: if (!f.canWrite()) {
182: throw new ContextError("PropertiesDPContext.writeFile(): "
183: + "cannot write to file: " + f);
184: }
185:
186: try {
187: BufferedReader br = new BufferedReader(new StringReader(s));
188: BufferedWriter bw = new BufferedWriter(new FileWriter(f));
189: String line = null;
190: while ((line = br.readLine()) != null) {
191: bw.write(line);
192: bw.newLine();
193: }
194: bw.flush();
195: bw.close();
196: br.close();
197: } catch (IOException ioe) {
198: throw new ContextError("PropertiesDPContext.writeFile(): ",
199: ioe);
200: }
201: }
202:
203: public long getDPUserDocumentLastModified() {
204: return -1;
205: }
206:
207: public long getDPDocumentLastModified(String name) {
208: return -1;
209: }
210:
211: public long getDPUserDocumentLastRead() {
212: return -1;
213: }
214:
215: public long getDPDocumentLastRead(String name) {
216: return -1;
217: }
218:
219: private static class DPFileComparator implements Comparator {
220: public int compare(Object o1, Object o2) {
221: File f1 = (File) o1;
222: File f2 = (File) o2;
223:
224: return f1.toString().compareTo(f2.toString());
225: }
226: }
227:
228: private static class DPDocumentFilenameFilter implements
229: FilenameFilter {
230: public boolean accept(File dir, String name) {
231: if (name.equals("dp-user.xml")) {
232: //
233: // skip the user level document here
234: //
235:
236: return false;
237: }
238:
239: if (!name.startsWith("dp-")) {
240: return false;
241: }
242:
243: if (!name.endsWith(".xml")) {
244: return false;
245: }
246:
247: return true;
248: }
249: }
250: }
|