001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.portlets.rpad.simple;
018:
019: import org.apache.commons.logging.Log;
020: import org.apache.commons.logging.LogFactory;
021:
022: import java.io.File;
023: import java.io.FileInputStream;
024: import java.io.FileNotFoundException;
025: import java.io.IOException;
026: import java.net.MalformedURLException;
027: import java.net.URL;
028: import java.util.ArrayList;
029: import java.util.Iterator;
030: import java.util.List;
031:
032: import javax.xml.parsers.SAXParser;
033: import javax.xml.parsers.SAXParserFactory;
034:
035: import org.apache.jetspeed.portlets.rpad.PortletApplication;
036: import org.apache.jetspeed.portlets.rpad.Repository;
037: import org.xml.sax.InputSource;
038:
039: public class SimpleRepository implements Repository {
040: /**
041: * Logger for this class
042: */
043: private static final Log log = LogFactory
044: .getLog(SimpleRepository.class);
045:
046: private String name;
047:
048: private String configPath;
049:
050: private List portletApplications;
051:
052: private boolean available;
053:
054: public SimpleRepository() {
055: available = false;
056: }
057:
058: public void init() {
059: if (log.isDebugEnabled()) {
060: log.debug("Loading a repository: " + configPath);
061: }
062:
063: if (configPath == null) {
064: throw new IllegalStateException(
065: "The configuration path is null.");
066: }
067: //load config
068: try {
069: InputSource inputSource = getInputSource(configPath);
070: if (inputSource == null) {
071: log.error("Failed to load: " + configPath);
072: return;
073: }
074: SAXParserFactory spfactory = SAXParserFactory.newInstance();
075: SAXParser parser = spfactory.newSAXParser();
076: SimpleConfigHandler repoConfigHandler = new SimpleConfigHandler();
077: parser.parse(inputSource, repoConfigHandler);
078: portletApplications = repoConfigHandler
079: .getPortletApplications();
080: if (portletApplications == null) {
081: if (log.isDebugEnabled()) {
082: log.debug("No portlet application.");
083: }
084: portletApplications = new ArrayList();
085: }
086: available = true;
087: } catch (Exception e) {
088: log.error("Could not load a repository.", e);
089: portletApplications = new ArrayList();
090: available = false;
091: }
092: }
093:
094: protected InputSource getInputSource(String path) {
095: if (path.startsWith("http:") || path.startsWith("https:")) {
096: try {
097: URL url = new URL(path);
098: return new InputSource(url.openStream());
099: } catch (MalformedURLException e) {
100: log.error("Wrong url: " + path, e);
101: } catch (IOException e) {
102: log.error("Could not load " + path, e);
103: }
104: } else if (path.startsWith("file:")) {
105: try {
106: return new InputSource(new FileInputStream(new File(
107: path.substring(5))));
108: } catch (FileNotFoundException e) {
109: log.error("Could not load " + path, e);
110: }
111: }
112: return null;
113: }
114:
115: public PortletApplication getPortletApplication(String groupId,
116: String artifactId, String version, String packaging) {
117: if (groupId != null && artifactId != null && version != null
118: && packaging != null) {
119: Iterator ite = portletApplications.iterator();
120: while (ite.hasNext()) {
121: PortletApplication portletApp = (PortletApplication) ite
122: .next();
123: if (groupId.equals(portletApp.getGroupId())
124: && artifactId
125: .equals(portletApp.getArtifactId())
126: && version.equals(portletApp.getVersion())
127: && packaging.equals(portletApp.getPackaging())) {
128: return portletApp;
129: }
130: }
131: }
132: return null;
133: }
134:
135: /**
136: * @return the configPath
137: */
138: public String getConfigPath() {
139: return configPath;
140: }
141:
142: /**
143: * @param configPath the configPath to set
144: */
145: public void setConfigPath(String configPath) {
146: this .configPath = configPath;
147: }
148:
149: /**
150: * @return the portletApplications
151: */
152: public List getPortletApplications() {
153: return portletApplications;
154: }
155:
156: /**
157: * @return the name
158: */
159: public String getName() {
160: return name;
161: }
162:
163: /**
164: * @param name the name to set
165: */
166: public void setName(String name) {
167: this .name = name;
168: }
169:
170: public String toXMLString() {
171: return " <repository>\n" //
172: + " <name>"
173: + getName()
174: + "</name>\n" //
175: + " <class name=\""
176: + "org.apache.jetspeed.portlets.rpad.simple.SimpleRepository"
177: + "\"/>\n" //
178: + " <property name=\"configPath\">"
179: + getConfigPath() + "</property>\n" //
180: + " </repository>\n";
181: }
182:
183: /**
184: * @return the available
185: */
186: public boolean isAvailable() {
187: return available;
188: }
189:
190: /**
191: * @param available the available to set
192: */
193: public void setAvailable(boolean available) {
194: this.available = available;
195: }
196: }
|