1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.portlets.rpad;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.xml.sax.SAXException;
22
23 import java.io.BufferedWriter;
24 import java.io.File;
25 import java.io.FileNotFoundException;
26 import java.io.FileOutputStream;
27 import java.io.IOException;
28 import java.io.OutputStreamWriter;
29 import java.io.UnsupportedEncodingException;
30 import java.util.ArrayList;
31 import java.util.Iterator;
32 import java.util.List;
33 import java.util.Map;
34
35 import javax.xml.parsers.ParserConfigurationException;
36 import javax.xml.parsers.SAXParser;
37 import javax.xml.parsers.SAXParserFactory;
38
39 public class RepositoryManager
40 {
41 /***
42 * Logger for this class
43 */
44 private static final Log log = LogFactory.getLog(RepositoryManager.class);
45
46 private String configFileName;
47
48 private Map repositories;
49
50 private static RepositoryManager repositoryManager;
51
52 public static void init(String configFileName) throws RPADException
53 {
54 repositoryManager = new RepositoryManager(configFileName);
55 }
56
57 public static RepositoryManager getInstance()
58 {
59 if (repositoryManager == null)
60 {
61 throw new IllegalStateException(
62 "init() needs to be called before getInstance().");
63 }
64 return repositoryManager;
65 }
66
67 public RepositoryManager(String configFileName) throws RPADException
68 {
69 this.configFileName = configFileName;
70 load();
71 }
72
73 protected void load() throws RPADException
74 {
75 try
76 {
77 SAXParserFactory spfactory = SAXParserFactory.newInstance();
78 SAXParser parser = spfactory.newSAXParser();
79 RepositoryConfigHandler repoConfigHandler = new RepositoryConfigHandler();
80 parser.parse(new File(configFileName), repoConfigHandler);
81 repositories = repoConfigHandler.getRepositories();
82 }
83 catch (ParserConfigurationException e)
84 {
85 throw new RPADException("Could not configure a parser.", e);
86 }
87 catch (SAXException e)
88 {
89 throw new RPADException("An exception occurrs on SAX parser.", e);
90 }
91 catch (IOException e)
92 {
93 throw new RPADException(
94 "An exception occurrs when accessing a configuration file: "
95 + configFileName, e);
96 }
97 }
98
99 public void reload() throws RPADException
100 {
101 synchronized (repositories)
102 {
103 load();
104 }
105 }
106
107 public void addRepository(String name, Repository repository)
108 throws RPADException
109 {
110 synchronized (repositories)
111 {
112 if (repositories.containsKey(name))
113 {
114 throw new RPADException(name + "exists.");
115 }
116 repositories.put(name, repository);
117 store();
118 }
119 }
120
121 public Repository getRepository(String name)
122 {
123 return (Repository) repositories.get(name);
124 }
125
126 public void removeRepository(String name) throws RPADException
127 {
128 synchronized (repositories)
129 {
130 if (!repositories.containsKey(name))
131 {
132 throw new RPADException(name + "does not exist.");
133 }
134 repositories.remove(name);
135 store();
136 }
137 }
138
139 public List getRepositories()
140 {
141 return new ArrayList(repositories.values());
142 }
143
144 public void store() throws RPADException
145 {
146 synchronized (repositories)
147 {
148 BufferedWriter writer = null;
149 try
150 {
151 try
152 {
153 writer = new BufferedWriter(new OutputStreamWriter(
154 new FileOutputStream(configFileName), "UTF-8"));
155 }
156 catch (UnsupportedEncodingException e)
157 {
158 writer = new BufferedWriter(new OutputStreamWriter(
159 new FileOutputStream(configFileName)));
160 }
161 writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
162 writer.write("<repositories>\n");
163 for (Iterator i = repositories.entrySet().iterator(); i
164 .hasNext();)
165 {
166 Map.Entry entry = (Map.Entry) i.next();
167 if (log.isDebugEnabled())
168 {
169 log.debug("Storing a repository: " + entry.getKey());
170 }
171
172 Repository repo = (Repository) entry.getValue();
173 writer.write(repo.toXMLString());
174 }
175 writer.write("</repositories>\n");
176 writer.flush();
177 }
178 catch (FileNotFoundException e)
179 {
180 throw new RPADException("Could not find " + configFileName, e);
181 }
182 catch (IOException e)
183 {
184 throw new RPADException("Could not write " + configFileName, e);
185 }
186 finally
187 {
188 if (writer != null)
189 {
190 try
191 {
192 writer.close();
193 }
194 catch (IOException e)
195 {
196 }
197 }
198 }
199 }
200 }
201
202 public List getPortletApplications()
203 {
204 ArrayList list = new ArrayList();
205 for (Iterator i = repositories.entrySet().iterator(); i.hasNext();)
206 {
207 Map.Entry entry = (Map.Entry) i.next();
208 Repository repo = (Repository) entry.getValue();
209 if (repo.isAvailable())
210 {
211 List portlets = repo.getPortletApplications();
212 if (portlets != null)
213 {
214 list.addAll(portlets);
215 }
216 }
217 }
218 return list;
219 }
220
221 public List getPortletApplications(String name)
222 {
223 ArrayList list = new ArrayList();
224
225 Repository repo = getRepository(name);
226 if (repo != null && repo.isAvailable())
227 {
228 List portlets = repo.getPortletApplications();
229 if (portlets != null)
230 {
231 list.addAll(portlets);
232 }
233 }
234 return list;
235 }
236
237
238 }