KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > pooka > resource > DisklessResourceManager


1 package net.suberic.pooka.resource;
2
3 import net.suberic.util.*;
4 import net.suberic.pooka.*;
5 import net.suberic.pooka.ssl.*;
6
7 import javax.activation.*;
8 import java.io.*;
9 import java.util.*;
10 import java.net.*;
11
12 /**
13  * A PookaResourceManager which uses no files.
14  */

15 public class DisklessResourceManager extends ResourceManager {
16
17   /**
18    * Creates a VariableBundle to be used.
19    */

20   public VariableBundle createVariableBundle(String JavaDoc fileName, VariableBundle defaults) {
21     return defaults;
22   }
23
24   /**
25    * Creates a MailcapCommandMap to be used.
26    */

27   public MailcapCommandMap createMailcap(String JavaDoc fileName) {
28     return new FullMailcapCommandMap();
29   }
30
31   /**
32    * Creates a PookaTrustManager.
33    */

34   public PookaTrustManager createPookaTrustManager(javax.net.ssl.TrustManager[] pTrustManagers, String JavaDoc fileName) {
35     return new PookaTrustManager(pTrustManagers, null, false);
36   }
37
38   /**
39    * Creates an output file which includes only resources that are appropriate
40    * to a Diskless client.
41    */

42   public static void exportResources(File pOutputFile, boolean pIncludePasswords) throws IOException {
43     VariableBundle sourceBundle = Pooka.getResources();
44
45     pOutputFile.createNewFile();
46     VariableBundle newWritableProperties = new VariableBundle(pOutputFile, null);
47     
48     // first go through and edit out the inappropriate stores.
49

50     List allStores = Pooka.getStoreManager().getStoreList();
51     List toRemoveList = new ArrayList();
52     List keepList = new ArrayList();
53
54     Iterator iter = allStores.iterator();
55     while (iter.hasNext()) {
56       // if they're not imap, exclude them. if they are imap, set them not
57
// to cache.
58
StoreInfo current = (StoreInfo) iter.next();
59
60       if (current.getProtocol() != null && current.getProtocol().toLowerCase().startsWith("imap")) {
61         newWritableProperties.setProperty(current.getStoreProperty() + ".cachingEnabled", "false");
62         keepList.add(current.getStoreID());
63       } else {
64         toRemoveList.add(current.getStoreID());
65       }
66     }
67     
68     //Enumeration names = newWritableProperties.propertyNames();
69
//Enumeration names = sourceBundle.getWritableProperties().propertyNames();
70
Enumeration names = sourceBundle.getProperties().propertyNames();
71
72     while (names.hasMoreElements()) {
73       String JavaDoc current = (String JavaDoc) names.nextElement();
74       
75       boolean keep = true;
76       if (current.startsWith("Store")) {
77         if ((! pIncludePasswords) && current.endsWith("password")) {
78           keep = false;
79         } else if (current.endsWith("cachingEnabled")) {
80           keep = false;
81         }
82
83         for (int i = 0; keep && i < toRemoveList.size(); i++) {
84           if (current.startsWith("Store." + (String JavaDoc) toRemoveList.get(i))) {
85             keep = false;
86           }
87         }
88       }
89
90       if (keep) {
91         newWritableProperties.setProperty(current, sourceBundle.getProperty(current));
92       }
93
94     }
95
96     // don't use local files.
97
newWritableProperties.setProperty("Pooka.useLocalFiles", "false");
98
99     // put only the kept stores in the store list.
100
newWritableProperties.setProperty("Store", VariableBundle.convertToString(keepList));
101
102     //FileOutputStream outStream = new FileOutputStream(pOutputFile);
103

104     //newWritableProperties.setSaveFile(pOutputFile);
105
newWritableProperties.saveProperties();
106     
107     //outStream.close();
108

109     
110   }
111
112   /**
113    * Gets a resource for reading. pFileName could be a URL or a file name
114    * or some similar identifier that the
115    */

116   public java.io.InputStream JavaDoc getInputStream(String JavaDoc pFileName)
117     throws java.io.IOException JavaDoc {
118     try {
119       URL url = new URL(pFileName);
120       return url.openStream();
121     } catch (MalformedURLException mue) {
122       throw new IOException("Error opening URL: " + mue.getMessage());
123     }
124   }
125
126   public java.io.OutputStream JavaDoc getOutputStream(String JavaDoc pFileName)
127     throws java.io.IOException JavaDoc {
128     // no writing to streams in this one.
129
throw new IOException("Diskless mode: no file modification available.");
130   }
131   
132   /**
133    * Creates an appropriate FolderInfo for the given StoreInfo.
134    */

135   public FolderInfo createFolderInfo(StoreInfo pStore, String JavaDoc pName) {
136     String JavaDoc storeProperty = pStore.getStoreProperty();
137     if (pStore.isPopStore() && pName.equalsIgnoreCase("INBOX")) {
138       return new PopInboxFolderInfo(pStore, pName);
139     } else if (Pooka.getProperty(storeProperty + ".protocol", "mbox").equalsIgnoreCase("imap")) {
140       return new UIDFolderInfo(pStore, pName);
141     } else {
142       return new FolderInfo(pStore, pName);
143     }
144   }
145   
146 }
147
Popular Tags