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      //added static modifyer for hot deploy
64      private static 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      	//added for hot deploy
98      	fragments = new HashMap();
99          root = registry.build(config);
100         //added for hot deploy
101         PageRegistry.setRootFragment();
102         
103         if (log.isDebugEnabled())
104         {
105             log.debug("Dumping complete navigation tree created of the object model...");
106             log.debug(root.getNavigation().toString());
107         }
108     }
109 
110     public RootFragment getRootFragment()
111     {
112         return root;
113     }
114 
115     public Fragment getFragment(String id) {
116         return (Fragment)fragments.get(id);
117     }
118 
119     public void addFragment(Fragment fragment) throws Exception {
120         String id = fragment.getId();
121         if(! fragments.containsKey(id)) {
122             
123             fragments.put(id, fragment);
124 
125         } else {
126 
127             String msg = "Fragment with this name "+id+" already exists in the pageregistry.xml.";
128             log.error(msg);
129             throw new Exception(msg);
130         }
131 
132     }
133 
134     private void load() throws Exception
135     {
136         String filename = CONFIG_FILE;
137         
138         File f = new File(filename);
139         if (!f.isAbsolute())
140            filename = servletContext.getRealPath(filename);
141 
142         Unmarshaller unmarshaller = new Unmarshaller(this.mapping);
143 
144         registry = (PortalImpl)unmarshaller.unmarshal(new FileReader(filename));
145     }
146 
147 }