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;
018:
019: import org.apache.commons.logging.Log;
020: import org.apache.commons.logging.LogFactory;
021: import org.xml.sax.SAXException;
022:
023: import java.io.BufferedWriter;
024: import java.io.File;
025: import java.io.FileNotFoundException;
026: import java.io.FileOutputStream;
027: import java.io.IOException;
028: import java.io.OutputStreamWriter;
029: import java.io.UnsupportedEncodingException;
030: import java.util.ArrayList;
031: import java.util.Iterator;
032: import java.util.List;
033: import java.util.Map;
034:
035: import javax.xml.parsers.ParserConfigurationException;
036: import javax.xml.parsers.SAXParser;
037: import javax.xml.parsers.SAXParserFactory;
038:
039: public class RepositoryManager {
040: /**
041: * Logger for this class
042: */
043: private static final Log log = LogFactory
044: .getLog(RepositoryManager.class);
045:
046: private String configFileName;
047:
048: private Map repositories;
049:
050: private static RepositoryManager repositoryManager;
051:
052: public static void init(String configFileName) throws RPADException {
053: repositoryManager = new RepositoryManager(configFileName);
054: }
055:
056: public static RepositoryManager getInstance() {
057: if (repositoryManager == null) {
058: throw new IllegalStateException(
059: "init() needs to be called before getInstance().");
060: }
061: return repositoryManager;
062: }
063:
064: public RepositoryManager(String configFileName)
065: throws RPADException {
066: this .configFileName = configFileName;
067: load();
068: }
069:
070: protected void load() throws RPADException {
071: try {
072: SAXParserFactory spfactory = SAXParserFactory.newInstance();
073: SAXParser parser = spfactory.newSAXParser();
074: RepositoryConfigHandler repoConfigHandler = new RepositoryConfigHandler();
075: parser.parse(new File(configFileName), repoConfigHandler);
076: repositories = repoConfigHandler.getRepositories();
077: } catch (ParserConfigurationException e) {
078: throw new RPADException("Could not configure a parser.", e);
079: } catch (SAXException e) {
080: throw new RPADException(
081: "An exception occurrs on SAX parser.", e);
082: } catch (IOException e) {
083: throw new RPADException(
084: "An exception occurrs when accessing a configuration file: "
085: + configFileName, e);
086: }
087: }
088:
089: public void reload() throws RPADException {
090: synchronized (repositories) {
091: load();
092: }
093: }
094:
095: public void addRepository(String name, Repository repository)
096: throws RPADException {
097: synchronized (repositories) {
098: if (repositories.containsKey(name)) {
099: throw new RPADException(name + "exists.");
100: }
101: repositories.put(name, repository);
102: store();
103: }
104: }
105:
106: public Repository getRepository(String name) {
107: return (Repository) repositories.get(name);
108: }
109:
110: public void removeRepository(String name) throws RPADException {
111: synchronized (repositories) {
112: if (!repositories.containsKey(name)) {
113: throw new RPADException(name + "does not exist.");
114: }
115: repositories.remove(name);
116: store();
117: }
118: }
119:
120: public List getRepositories() {
121: return new ArrayList(repositories.values());
122: }
123:
124: public void store() throws RPADException {
125: synchronized (repositories) {
126: BufferedWriter writer = null;
127: try {
128: try {
129: writer = new BufferedWriter(new OutputStreamWriter(
130: new FileOutputStream(configFileName),
131: "UTF-8"));
132: } catch (UnsupportedEncodingException e) {
133: writer = new BufferedWriter(new OutputStreamWriter(
134: new FileOutputStream(configFileName)));
135: }
136: writer
137: .write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
138: writer.write("<repositories>\n");
139: for (Iterator i = repositories.entrySet().iterator(); i
140: .hasNext();) {
141: Map.Entry entry = (Map.Entry) i.next();
142: if (log.isDebugEnabled()) {
143: log.debug("Storing a repository: "
144: + entry.getKey());
145: }
146:
147: Repository repo = (Repository) entry.getValue();
148: writer.write(repo.toXMLString());
149: }
150: writer.write("</repositories>\n");
151: writer.flush();
152: } catch (FileNotFoundException e) {
153: throw new RPADException("Could not find "
154: + configFileName, e);
155: } catch (IOException e) {
156: throw new RPADException("Could not write "
157: + configFileName, e);
158: } finally {
159: if (writer != null) {
160: try {
161: writer.close();
162: } catch (IOException e) {
163: }
164: }
165: }
166: }
167: }
168:
169: public List getPortletApplications() {
170: ArrayList list = new ArrayList();
171: for (Iterator i = repositories.entrySet().iterator(); i
172: .hasNext();) {
173: Map.Entry entry = (Map.Entry) i.next();
174: Repository repo = (Repository) entry.getValue();
175: if (repo.isAvailable()) {
176: List portlets = repo.getPortletApplications();
177: if (portlets != null) {
178: list.addAll(portlets);
179: }
180: }
181: }
182: return list;
183: }
184:
185: public List getPortletApplications(String name) {
186: ArrayList list = new ArrayList();
187:
188: Repository repo = getRepository(name);
189: if (repo != null && repo.isAvailable()) {
190: List portlets = repo.getPortletApplications();
191: if (portlets != null) {
192: list.addAll(portlets);
193: }
194: }
195: return list;
196: }
197:
198: //TODO search
199: }
|