001: package net.suberic.pooka.resource;
002:
003: import net.suberic.util.*;
004: import net.suberic.pooka.*;
005: import net.suberic.pooka.ssl.*;
006:
007: import javax.activation.*;
008: import java.io.*;
009: import java.util.*;
010: import java.net.*;
011:
012: /**
013: * A PookaResourceManager which uses no files.
014: */
015: public class DisklessResourceManager extends ResourceManager {
016:
017: /**
018: * Creates a VariableBundle to be used.
019: */
020: public VariableBundle createVariableBundle(String fileName,
021: VariableBundle defaults) {
022: return defaults;
023: }
024:
025: /**
026: * Creates a MailcapCommandMap to be used.
027: */
028: public MailcapCommandMap createMailcap(String fileName) {
029: return new FullMailcapCommandMap();
030: }
031:
032: /**
033: * Creates a PookaTrustManager.
034: */
035: public PookaTrustManager createPookaTrustManager(
036: javax.net.ssl.TrustManager[] pTrustManagers, String fileName) {
037: return new PookaTrustManager(pTrustManagers, null, false);
038: }
039:
040: /**
041: * Creates an output file which includes only resources that are appropriate
042: * to a Diskless client.
043: */
044: public static void exportResources(File pOutputFile,
045: boolean pIncludePasswords) throws IOException {
046: VariableBundle sourceBundle = Pooka.getResources();
047:
048: pOutputFile.createNewFile();
049: VariableBundle newWritableProperties = new VariableBundle(
050: pOutputFile, null);
051:
052: // first go through and edit out the inappropriate stores.
053:
054: List allStores = Pooka.getStoreManager().getStoreList();
055: List toRemoveList = new ArrayList();
056: List keepList = new ArrayList();
057:
058: Iterator iter = allStores.iterator();
059: while (iter.hasNext()) {
060: // if they're not imap, exclude them. if they are imap, set them not
061: // to cache.
062: StoreInfo current = (StoreInfo) iter.next();
063:
064: if (current.getProtocol() != null
065: && current.getProtocol().toLowerCase().startsWith(
066: "imap")) {
067: newWritableProperties.setProperty(current
068: .getStoreProperty()
069: + ".cachingEnabled", "false");
070: keepList.add(current.getStoreID());
071: } else {
072: toRemoveList.add(current.getStoreID());
073: }
074: }
075:
076: //Enumeration names = newWritableProperties.propertyNames();
077: //Enumeration names = sourceBundle.getWritableProperties().propertyNames();
078: Enumeration names = sourceBundle.getProperties()
079: .propertyNames();
080:
081: while (names.hasMoreElements()) {
082: String current = (String) names.nextElement();
083:
084: boolean keep = true;
085: if (current.startsWith("Store")) {
086: if ((!pIncludePasswords)
087: && current.endsWith("password")) {
088: keep = false;
089: } else if (current.endsWith("cachingEnabled")) {
090: keep = false;
091: }
092:
093: for (int i = 0; keep && i < toRemoveList.size(); i++) {
094: if (current.startsWith("Store."
095: + (String) toRemoveList.get(i))) {
096: keep = false;
097: }
098: }
099: }
100:
101: if (keep) {
102: newWritableProperties.setProperty(current, sourceBundle
103: .getProperty(current));
104: }
105:
106: }
107:
108: // don't use local files.
109: newWritableProperties.setProperty("Pooka.useLocalFiles",
110: "false");
111:
112: // put only the kept stores in the store list.
113: newWritableProperties.setProperty("Store", VariableBundle
114: .convertToString(keepList));
115:
116: //FileOutputStream outStream = new FileOutputStream(pOutputFile);
117:
118: //newWritableProperties.setSaveFile(pOutputFile);
119: newWritableProperties.saveProperties();
120:
121: //outStream.close();
122:
123: }
124:
125: /**
126: * Gets a resource for reading. pFileName could be a URL or a file name
127: * or some similar identifier that the
128: */
129: public java.io.InputStream getInputStream(String pFileName)
130: throws java.io.IOException {
131: try {
132: URL url = new URL(pFileName);
133: return url.openStream();
134: } catch (MalformedURLException mue) {
135: throw new IOException("Error opening URL: "
136: + mue.getMessage());
137: }
138: }
139:
140: public java.io.OutputStream getOutputStream(String pFileName)
141: throws java.io.IOException {
142: // no writing to streams in this one.
143: throw new IOException(
144: "Diskless mode: no file modification available.");
145: }
146:
147: }
|