1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.portlets.rpad.simple;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21
22 import java.io.File;
23 import java.io.FileInputStream;
24 import java.io.FileNotFoundException;
25 import java.io.IOException;
26 import java.net.MalformedURLException;
27 import java.net.URL;
28 import java.util.ArrayList;
29 import java.util.Iterator;
30 import java.util.List;
31
32 import javax.xml.parsers.SAXParser;
33 import javax.xml.parsers.SAXParserFactory;
34
35 import org.apache.jetspeed.portlets.rpad.PortletApplication;
36 import org.apache.jetspeed.portlets.rpad.Repository;
37 import org.xml.sax.InputSource;
38
39 public class SimpleRepository implements Repository
40 {
41 /***
42 * Logger for this class
43 */
44 private static final Log log = LogFactory.getLog(SimpleRepository.class);
45
46 private String name;
47
48 private String configPath;
49
50 private List portletApplications;
51
52 private boolean available;
53
54 public SimpleRepository()
55 {
56 available = false;
57 }
58
59 public void init()
60 {
61 if (log.isDebugEnabled())
62 {
63 log.debug("Loading a repository: " + configPath);
64 }
65
66 if (configPath == null)
67 {
68 throw new IllegalStateException("The configuration path is null.");
69 }
70
71 try
72 {
73 InputSource inputSource = getInputSource(configPath);
74 if (inputSource == null)
75 {
76 log.error("Failed to load: " + configPath);
77 return;
78 }
79 SAXParserFactory spfactory = SAXParserFactory.newInstance();
80 SAXParser parser = spfactory.newSAXParser();
81 SimpleConfigHandler repoConfigHandler = new SimpleConfigHandler();
82 parser.parse(inputSource, repoConfigHandler);
83 portletApplications = repoConfigHandler.getPortletApplications();
84 if (portletApplications == null)
85 {
86 if (log.isDebugEnabled())
87 {
88 log.debug("No portlet application.");
89 }
90 portletApplications = new ArrayList();
91 }
92 available = true;
93 }
94 catch (Exception e)
95 {
96 log.error("Could not load a repository.", e);
97 portletApplications = new ArrayList();
98 available = false;
99 }
100 }
101
102 protected InputSource getInputSource(String path)
103 {
104 if (path.startsWith("http:") || path.startsWith("https:"))
105 {
106 try
107 {
108 URL url = new URL(path);
109 return new InputSource(url.openStream());
110 }
111 catch (MalformedURLException e)
112 {
113 log.error("Wrong url: " + path, e);
114 }
115 catch (IOException e)
116 {
117 log.error("Could not load " + path, e);
118 }
119 }
120 else if (path.startsWith("file:"))
121 {
122 try
123 {
124 return new InputSource(new FileInputStream(new File(path
125 .substring(5))));
126 }
127 catch (FileNotFoundException e)
128 {
129 log.error("Could not load " + path, e);
130 }
131 }
132 return null;
133 }
134
135 public PortletApplication getPortletApplication(String groupId,
136 String artifactId, String version, String packaging)
137 {
138 if (groupId != null && artifactId != null && version != null
139 && packaging != null)
140 {
141 Iterator ite = portletApplications.iterator();
142 while (ite.hasNext())
143 {
144 PortletApplication portletApp = (PortletApplication) ite.next();
145 if (groupId.equals(portletApp.getGroupId())
146 && artifactId.equals(portletApp.getArtifactId())
147 && version.equals(portletApp.getVersion())
148 && packaging.equals(portletApp.getPackaging()))
149 {
150 return portletApp;
151 }
152 }
153 }
154 return null;
155 }
156
157 /***
158 * @return the configPath
159 */
160 public String getConfigPath()
161 {
162 return configPath;
163 }
164
165 /***
166 * @param configPath the configPath to set
167 */
168 public void setConfigPath(String configPath)
169 {
170 this.configPath = configPath;
171 }
172
173 /***
174 * @return the portletApplications
175 */
176 public List getPortletApplications()
177 {
178 return portletApplications;
179 }
180
181 /***
182 * @return the name
183 */
184 public String getName()
185 {
186 return name;
187 }
188
189 /***
190 * @param name the name to set
191 */
192 public void setName(String name)
193 {
194 this.name = name;
195 }
196
197 public String toXMLString()
198 {
199 return " <repository>\n"
200 + " <name>"
201 + getName()
202 + "</name>\n"
203 + " <class name=\""
204 + "org.apache.jetspeed.portlets.rpad.simple.SimpleRepository"
205 + "\"/>\n"
206 + " <property name=\"configPath\">"
207 + getConfigPath()
208 + "</property>\n"
209 + " </repository>\n";
210 }
211
212 /***
213 * @return the available
214 */
215 public boolean isAvailable()
216 {
217 return available;
218 }
219
220 /***
221 * @param available the available to set
222 */
223 public void setAvailable(boolean available)
224 {
225 this.available = available;
226 }
227 }