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