KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > pluto > portlet > admin > model > PortletEntityRegistryXao


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 JavaDoc;
19 import java.io.FileWriter JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.Iterator JavaDoc;
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 JavaDoc CONFIG_FILE = "WEB-INF/data/portletentityregistry.xml";
49     public final static String JavaDoc DEFAULT_MAPPING = "WEB-INF/data/xml/portletentitymapping.xml";
50     private final static String JavaDoc CLASS_NAME = "PortletEntityRegistryXao";
51     //Application elements within the registry
52
private Collection JavaDoc castorApplications = new ArrayList JavaDoc();
53
54     // Castor mapping file
55
private Mapping mapping = null;
56
57     /**
58      *
59      */

60     public PortletEntityRegistryXao() throws Exception JavaDoc {
61         super(CLASS_NAME);
62         init();
63     }
64     public void init () throws Exception JavaDoc
65     {
66          final String JavaDoc METHOD_NAME = "init()";
67
68         String JavaDoc _mapping = PlutoAdminContext.getInstance().getPlutoHome() + "/" + DEFAULT_MAPPING;
69
70         this.mapping = new Mapping();
71         try
72         {
73             this.mapping.loadMapping(_mapping);
74         }
75         catch (Exception JavaDoc 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 JavaDoc
86     {
87         final String JavaDoc METHOD_NAME = "save(AdminPortalImpl)";
88       String JavaDoc filename = PlutoAdminContext.getInstance().getPlutoHome() + "/" + CONFIG_FILE;
89         logDebug(METHOD_NAME, "Registry file to save: " + filename);
90
91         FileWriter JavaDoc writer = new FileWriter JavaDoc(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 JavaDoc
101     {
102         final String JavaDoc METHOD_NAME = "load()";
103
104         String JavaDoc 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 JavaDoc(filename));
111         castorApplications = apps.getCastorApplications();
112         return apps;
113     }
114
115     public String JavaDoc toString()
116     {
117         return toString(0);
118     }
119
120     public String JavaDoc toString(int indent)
121     {
122         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(1000);
123         StringUtils.newLine(buffer,indent);
124         buffer.append(getClass().toString());
125         buffer.append(":");
126         StringUtils.newLine(buffer,indent);
127         buffer.append("{");
128         Iterator JavaDoc 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 JavaDoc getApplications() throws Exception JavaDoc {
146         String JavaDoc METHOD_NAME = "getApplications()";
147         logMethodStart(METHOD_NAME);
148         Collection JavaDoc 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 JavaDoc castorId) throws Exception JavaDoc {
159         PortletApplicationEntityImpl app = null;
160         Collection JavaDoc apps = getApplications();
161         Iterator JavaDoc 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 JavaDoc getPortletPreferences(String JavaDoc appId, String JavaDoc portletId) throws Exception JavaDoc {
173         Collection JavaDoc list = null;
174         PortletApplicationEntityImpl app = getApplication(appId);
175         Collection JavaDoc plets = app.getCastorPortlets();
176         Iterator JavaDoc 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 JavaDoc appContext) {
188         final String JavaDoc METHOD_NAME = "applicationExists(appContext)";
189         boolean exists = false;
190         Collection JavaDoc apps = null;
191         try {
192             apps = getApplications();
193         } catch (Exception JavaDoc e) {
194         logError(METHOD_NAME, e);
195             throw new PlutoAdminException(e);
196         }
197         Iterator JavaDoc 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 }
208
Popular Tags