View Javadoc

1   /*
2    * Copyright 2003,2004 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  /* 
17  
18   */
19  
20  package org.apache.pluto.portalImpl.services.portletentityregistry;
21  
22  import java.io.File;
23  import java.io.FileReader;
24  import java.io.FileWriter;
25  import java.io.IOException;
26  import java.util.HashMap;
27  import java.util.Iterator;
28  import java.util.Map;
29  
30  import javax.servlet.ServletConfig;
31  import javax.servlet.ServletContext;
32  
33  import org.apache.pluto.om.common.ObjectID;
34  import org.apache.pluto.om.entity.PortletApplicationEntity;
35  import org.apache.pluto.om.entity.PortletApplicationEntityList;
36  import org.apache.pluto.om.entity.PortletEntity;
37  import org.apache.pluto.portalImpl.om.entity.impl.PortletApplicationEntityListImpl;
38  import org.apache.pluto.portalImpl.services.log.Log;
39  import org.apache.pluto.portalImpl.util.Properties;
40  import org.apache.pluto.services.log.Logger;
41  import org.exolab.castor.mapping.Mapping;
42  import org.exolab.castor.xml.Marshaller;
43  import org.exolab.castor.xml.Unmarshaller;
44  
45  /***
46   * A simple XML Castor file based implementation of the
47   * <code>PortletEntityRegistryService</config>.
48   *
49   * <p>This store persit the PortletEntityRegistry informations.</p>
50   *
51   */
52  
53  public class PortletEntityRegistryServiceFileImpl extends PortletEntityRegistryService
54  {
55  
56  
57      // default configuration values
58      public final static String CONFIG_FILE              = "WEB-INF/data/portletentityregistry.xml";
59      public final static String DEFAULT_MAPPING          = "WEB-INF/data/xml/portletentitymapping.xml";
60      // configuration keys
61      protected final static String CONFIG_MAPPING          = "mapping.configfile";
62  
63      // Castor mapping file
64      protected Mapping mapping = null;
65      // Servlet Context
66      protected ServletContext servletContext = null;
67      private Logger log = null;
68      // Registry
69      protected PortletApplicationEntityListImpl registry = null;
70      
71      // Helper lists and hashtables to access the data as fast as possible
72      protected Map portletEntitiesKeyObjectID = new HashMap();
73  
74      public void init (ServletConfig servletConfig, Properties properties) throws Exception
75      {
76          servletContext = servletConfig.getServletContext();
77          log = Log.getService().getLogger(getClass());
78  
79          String _mapping = properties.getString(CONFIG_MAPPING, DEFAULT_MAPPING);
80          File f = new File(_mapping);
81          if (!f.isAbsolute())
82              _mapping = servletContext.getRealPath(_mapping);
83  
84          this.mapping = new Mapping();
85          try
86          {
87              this.mapping.loadMapping(_mapping);
88          }
89          catch (Exception e)
90          {
91              log.error("Failed to load mapping file "+_mapping,e);
92              throw e;
93          }
94      
95          load();
96      }
97  
98      public PortletApplicationEntityList getPortletApplicationEntityList()
99      {
100         return registry;
101     }
102 
103     public PortletEntity getPortletEntity(ObjectID id)
104     {
105         return (PortletEntity)portletEntitiesKeyObjectID.get(id.toString());
106     }
107 
108     public void store() throws IOException
109     {
110         String filename = CONFIG_FILE;
111         
112         File f = new File(filename);
113         if (!f.isAbsolute())
114            filename = servletContext.getRealPath(filename);
115         
116         FileWriter writer = new FileWriter(filename);
117         
118         try {
119             Marshaller marshaller = new Marshaller(writer);
120 
121             marshaller.setMapping(this.mapping);
122 
123             registry.preStore(null);
124 
125             marshaller.marshal(registry);
126 
127             registry.postStore(null);
128         }
129         catch (org.exolab.castor.mapping.MappingException e)
130         {
131             e.printStackTrace(System.err);
132             throw new IOException(e.toString());
133         }
134         catch (org.exolab.castor.xml.ValidationException e)
135         {
136             e.printStackTrace(System.err);
137             throw new IOException(e.toString());
138         }
139         catch (org.exolab.castor.xml.MarshalException e)
140         {
141             e.printStackTrace(System.err);
142             throw new IOException(e.toString());
143         }
144         catch (Exception e)
145         {
146             e.printStackTrace(System.err);
147             throw new IOException(e.toString());
148         }
149     }
150 
151     public void load() throws IOException
152     {
153         _load();
154 
155         if (log.isDebugEnabled())
156         {
157             log.debug("Dumping complete object model description as it is read from the xml file...");
158             log.debug(registry.toString());
159         }
160 
161         fill();
162     }
163 
164     public void refresh(PortletEntity portletEntity) {
165         portletEntitiesKeyObjectID.put(portletEntity.getId().toString(), portletEntity);
166     }
167 
168     private void _load() throws IOException
169     {
170         String filename = CONFIG_FILE;
171         
172         File f = new File(filename);
173         if (!f.isAbsolute())
174            filename = servletContext.getRealPath(filename);
175 
176         try {            
177 
178             Unmarshaller unmarshaller = new Unmarshaller(this.mapping);            
179 
180             registry = (PortletApplicationEntityListImpl)unmarshaller.unmarshal(new FileReader(filename));
181 
182             registry.postLoad(null);
183 
184             registry.preBuild(null);
185 
186             registry.postBuild(null);
187         }
188         catch (org.exolab.castor.mapping.MappingException e)
189         {
190             e.printStackTrace(System.err);
191             throw new IOException(e.toString());
192         }
193         catch (org.exolab.castor.xml.ValidationException e)
194         {
195             e.printStackTrace(System.err);
196             throw new IOException(e.toString());
197         }
198         catch (org.exolab.castor.xml.MarshalException e)
199         {
200             e.printStackTrace(System.err);
201             throw new IOException(e.toString());
202         }
203         catch (Exception e)
204         {
205             e.printStackTrace(System.err);
206             throw new IOException(e.toString());
207         }
208 
209     }
210 
211     private void fill()
212     {
213         Iterator iterator = registry.iterator();
214         while (iterator.hasNext())
215         {
216             PortletApplicationEntity appInst = (PortletApplicationEntity)iterator.next();
217 
218             // fill portletEntitiesKeyObjectID
219             Iterator portlets = appInst.getPortletEntityList().iterator();
220             while (portlets.hasNext())
221             {
222                 PortletEntity portletInst = (PortletEntity)portlets.next();                                
223                 portletEntitiesKeyObjectID.put(portletInst.getId().toString(), portletInst);
224 
225             }
226 
227         }
228 
229     }
230 
231     //additional methods
232 
233     public Map getPortletEntities()
234     {
235         return portletEntitiesKeyObjectID;
236     }
237 
238     //method added for hot deploy
239      public void postInit() throws Exception {
240        	PortletEntityRegistry.setPortletEntityRegistryService();
241      }
242 
243 }