1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
52 private Collection castorApplications = new ArrayList();
53
54
55 private Mapping mapping;
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 appContext) throws Exception {
159 PortletApplicationEntityImpl app = null;
160 Collection apps = getApplications();
161 Iterator iter = apps.iterator();
162 while(iter.hasNext()) {
163
164
165
166
167
168 app = (PortletApplicationEntityImpl) iter.next();
169 if (app.getDefinitionId().equalsIgnoreCase(appContext)) {
170 break;
171 }
172 }
173 return app;
174 }
175
176 public Collection getPortletPreferences(String appId, String portletId) throws Exception {
177 Collection list = null;
178 PortletApplicationEntityImpl app = getApplication(appId);
179 Collection plets = app.getCastorPortlets();
180 Iterator iter = plets.iterator();
181 while(iter.hasNext()) {
182 PortletEntityImpl plet = (PortletEntityImpl)iter.next();
183 if (plet.getCastorId().equalsIgnoreCase(portletId)) {
184 list = plet.getCastorPreferences();
185 break;
186 }
187 }
188 return list;
189 }
190
191 public boolean applicationExists(String appContext) {
192 final String METHOD_NAME = "applicationExists(appContext)";
193 boolean exists = false;
194 Collection apps = null;
195 try {
196 apps = getApplications();
197 } catch (Exception e) {
198 logError(METHOD_NAME, e);
199 throw new PlutoAdminException(e);
200 }
201 Iterator iter = apps.iterator();
202 while (iter.hasNext()) {
203 PortletApplicationEntityImpl app = (PortletApplicationEntityImpl) iter.next();
204 if (app.getDefinitionId().equalsIgnoreCase(appContext)) {
205 exists = true;
206 break;
207 }
208 }
209 return exists;
210 }
211 }