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