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.pageregistry;
21  
22  import java.io.File;
23  import java.io.FileReader;
24  import java.io.FileWriter;
25  import java.util.HashMap;
26  
27  import javax.servlet.ServletConfig;
28  import javax.servlet.ServletContext;
29  
30  import org.apache.pluto.portalImpl.aggregation.Fragment;
31  import org.apache.pluto.portalImpl.aggregation.RootFragment;
32  import org.apache.pluto.portalImpl.om.page.impl.PortalImpl;
33  import org.apache.pluto.portalImpl.services.log.Log;
34  import org.apache.pluto.portalImpl.util.Properties;
35  import org.apache.pluto.services.log.Logger;
36  import org.exolab.castor.mapping.Mapping;
37  import org.exolab.castor.xml.Marshaller;
38  import org.exolab.castor.xml.Unmarshaller;
39  
40  /***
41   * A simple XML Castor file based implementation of the
42   * <code>PageRegistryService</config>.
43   *
44   * <p>This store persit the PageRegistry informations.</p>
45   *
46   */
47  public class PageRegistryServiceFileImpl extends PageRegistryService
48  {
49  
50  
51      // default configuration values
52      public final static String CONFIG_FILE              = "WEB-INF/data/pageregistry.xml";
53      public final static String DEFAULT_MAPPING          = "WEB-INF/data/xml/pageregistrymapping.xml";
54      // configuration keys
55      private final static String CONFIG_MAPPING          = "mapping.configfile";
56  
57      // Castor mapping file
58      private Mapping mapping = null;
59      // Servlet Context
60      private ServletContext servletContext = null;
61      // Root element
62      private PortalImpl registry = null;
63      private RootFragment root = null;
64      private Logger log = null;
65  
66      private HashMap fragments = new HashMap();
67  
68      public void init (ServletConfig config, Properties properties) throws Exception
69      {        
70          servletContext = config.getServletContext();
71          log = Log.getService().getLogger(getClass());
72  
73          String _mapping = properties.getString(CONFIG_MAPPING, DEFAULT_MAPPING);
74          File f = new File(_mapping);
75          if (!f.isAbsolute())
76              _mapping = servletContext.getRealPath(_mapping);
77  
78          this.mapping = new Mapping();
79          try
80          {
81              this.mapping.loadMapping(_mapping);
82          }
83          catch (Exception e)
84          {
85              log.error("Failed to load mapping file "+_mapping,e);
86              throw e;
87          }
88  
89          load();
90  
91          if (log.isDebugEnabled())
92          {
93              log.debug("Dumping complete object model description as it is read from the xml file...");
94              log.debug(registry.toString());
95          }        
96      }
97  
98      public void postInit(ServletConfig config) throws Exception {
99          
100         root = registry.build(config);
101              
102         if (log.isDebugEnabled())
103         {
104             log.debug("Dumping complete navigation tree created of the object model...");
105             log.debug(root.getNavigation().toString());
106         }
107     }
108 
109     public RootFragment getRootFragment()
110     {
111         return root;
112     }
113 
114     public Fragment getFragment(String id) {
115         return (Fragment)fragments.get(id);
116     }
117 
118     public void addFragment(Fragment fragment) throws Exception {
119         String id = fragment.getId();
120         if(! fragments.containsKey(id)) {
121             
122             fragments.put(id, fragment);
123 
124         } else {
125 
126             String msg = "Fragment with this name "+id+" already exists in the pageregistry.xml.";
127             log.error(msg);
128             throw new Exception(msg);
129         }
130 
131     }
132 
133     private void store() throws Exception
134     {
135         String filename = CONFIG_FILE;
136         
137         File f = new File(filename);
138         if (!f.isAbsolute())
139            filename = servletContext.getRealPath(filename);
140         
141         FileWriter writer = new FileWriter(filename);
142         
143         Marshaller marshaller = new Marshaller(writer);
144         
145         marshaller.setMapping(this.mapping);
146         
147         marshaller.marshal(registry);
148     }
149 
150     private void load() throws Exception
151     {
152         String filename = CONFIG_FILE;
153         
154         File f = new File(filename);
155         if (!f.isAbsolute())
156            filename = servletContext.getRealPath(filename);
157 
158         Unmarshaller unmarshaller = new Unmarshaller(this.mapping);
159 
160         registry = (PortalImpl)unmarshaller.unmarshal(new FileReader(filename));
161     }
162 
163 }