View Javadoc

1   /*
2    * Copyright 2003,2004,2005 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.pluto.portlet.admin.model;
17  
18  import java.io.FileReader;
19  import java.io.FileWriter;
20  import java.util.ArrayList;
21  import java.util.Collection;
22  import java.util.Iterator;
23  
24  import org.apache.pluto.portalImpl.om.entity.impl.PortletApplicationEntityImpl;
25  import org.apache.pluto.portalImpl.om.entity.impl.PortletApplicationEntityListImpl;
26  import org.apache.pluto.portalImpl.om.entity.impl.PortletEntityImpl;
27  import org.apache.pluto.portlet.admin.BaseAdminObject;
28  import org.apache.pluto.portlet.admin.PlutoAdminException;
29  import org.apache.pluto.portlet.admin.util.PlutoAdminContext;
30  import org.apache.pluto.util.StringUtils;
31  import org.exolab.castor.mapping.Mapping;
32  import org.exolab.castor.xml.Marshaller;
33  import org.exolab.castor.xml.Unmarshaller;
34  
35  /***
36   * This class is used to access and store data in the portletentityregistry.xml file.
37   * It uses Castor and is loosely based on the
38   * org.apache.services.portletentityregistry.PortletEntityRegistryServiceFileImpl
39   * class in Pluto's 'portal' module.
40   *
41   * @author Craig Doremus
42   *
43   */
44  public class PortletEntityRegistryXao extends BaseAdminObject {
45  
46  
47      // default configuration values
48      public final static String CONFIG_FILE              = "WEB-INF/data/portletentityregistry.xml";
49      public final static String DEFAULT_MAPPING          = "WEB-INF/data/xml/portletentitymapping.xml";
50      private final static String CLASS_NAME = "PortletEntityRegistryXao";
51      //Application elements within the registry
52      private Collection castorApplications = new ArrayList();
53  
54      // Castor mapping file
55      private Mapping mapping = null;
56  
57  	/***
58  	 *
59  	 */
60  	public PortletEntityRegistryXao() throws Exception {
61  		super(CLASS_NAME);
62  		init();
63  	}
64      public void init () throws Exception
65      {
66      	 final String METHOD_NAME = "init()";
67  
68          String _mapping = PlutoAdminContext.getInstance().getPlutoHome() + "/" + DEFAULT_MAPPING;
69  
70          this.mapping = new Mapping();
71          try
72          {
73              this.mapping.loadMapping(_mapping);
74          }
75          catch (Exception e)
76          {
77              logError(METHOD_NAME, "Failed to load mapping file "+_mapping,e);
78              throw e;
79          }
80  
81      }
82  
83  
84  
85      public void save(PortletApplicationEntityListImpl apps) throws Exception
86      {
87      	final String METHOD_NAME = "save(AdminPortalImpl)";
88        String filename = PlutoAdminContext.getInstance().getPlutoHome() + "/" + CONFIG_FILE;
89      	logDebug(METHOD_NAME, "Registry file to save: " + filename);
90  
91          FileWriter writer = new FileWriter(filename);
92  
93          Marshaller marshaller = new Marshaller(writer);
94  
95          marshaller.setMapping(this.mapping);
96  
97          marshaller.marshal(apps);
98      }
99  
100     public PortletApplicationEntityListImpl load() throws Exception
101     {
102     	final String METHOD_NAME = "load()";
103 
104         String filename = PlutoAdminContext.getInstance().getPlutoHome() + "/" + CONFIG_FILE;
105       	logDebug(METHOD_NAME, "Registry file to load: " + filename);
106 
107         Unmarshaller unmarshaller = new Unmarshaller(this.mapping);
108         unmarshaller.setMapping(this.mapping);
109 
110         PortletApplicationEntityListImpl apps = (PortletApplicationEntityListImpl)unmarshaller.unmarshal(new FileReader(filename));
111         castorApplications = apps.getCastorApplications();
112         return apps;
113     }
114 
115     public String toString()
116     {
117         return toString(0);
118     }
119 
120     public String toString(int indent)
121     {
122         StringBuffer buffer = new StringBuffer(1000);
123         StringUtils.newLine(buffer,indent);
124         buffer.append(getClass().toString());
125         buffer.append(":");
126         StringUtils.newLine(buffer,indent);
127         buffer.append("{");
128         Iterator iterator = castorApplications.iterator();
129         if (iterator.hasNext()) {
130             StringUtils.newLine(buffer,indent);
131             buffer.append("Portlet Application Entities");
132             int count = castorApplications.size();
133             buffer.append("(");
134             buffer.append(count);
135             buffer.append("):");
136         }
137         while (iterator.hasNext()) {
138             buffer.append(((PortletApplicationEntityImpl)iterator.next()).toString(indent+2));
139         }
140         StringUtils.newLine(buffer,indent);
141         buffer.append("}");
142         return buffer.toString();
143     }
144 
145     public Collection getApplications() throws Exception {
146   		String METHOD_NAME = "getApplications()";
147   		logMethodStart(METHOD_NAME);
148   		Collection elist = null;
149   		PortletApplicationEntityListImpl per = load();
150   		logDebug(METHOD_NAME, "PER: " + per);
151   		if (per != null) {
152   			elist = per.getCastorApplications();
153   		}
154   		logMethodEnd(METHOD_NAME, elist);
155   		return elist;
156   	}
157 
158     public PortletApplicationEntityImpl getApplication(String castorId) throws Exception {
159     	PortletApplicationEntityImpl app = null;
160   		Collection apps = getApplications();
161   		Iterator iter = apps.iterator();
162   		while(iter.hasNext()) {
163   			PortletApplicationEntityImpl currApp = (PortletApplicationEntityImpl)iter.next();
164   			if (currApp.getCastorId().equalsIgnoreCase(castorId)) {
165   				app = currApp;
166   				break;
167   			}
168   		}
169   		return app;
170     }
171 
172     public Collection getPortletPreferences(String appId, String portletId) throws Exception {
173     	Collection list = null;
174     	PortletApplicationEntityImpl app = getApplication(appId);
175     	Collection plets = app.getCastorPortlets();
176     	Iterator iter = plets.iterator();
177     	while(iter.hasNext()) {
178     		PortletEntityImpl plet = (PortletEntityImpl)iter.next();
179     		if (plet.getCastorId().equalsIgnoreCase(portletId)) {
180     			list = plet.getCastorPreferences();
181     			break;
182     		}
183     	}
184     	return list;
185     }
186 
187     public boolean applicationExists(String appContext) {
188     	final String METHOD_NAME = "applicationExists(appContext)";
189     	boolean exists = false;
190     	Collection apps = null;
191 		try {
192 			apps = getApplications();
193 		} catch (Exception e) {
194   		logError(METHOD_NAME, e);
195 			throw new PlutoAdminException(e);
196 		}
197 		Iterator iter = apps.iterator();
198     	while (iter.hasNext()) {
199     		PortletApplicationEntityImpl app = (PortletApplicationEntityImpl) iter.next();
200     		if (app.getDefinitionId().equalsIgnoreCase(appContext)) {
201     			exists = true;
202     			break;
203     		}
204     	}
205     	return exists;
206     }
207 }