KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > pluto > portalImpl > services > pageregistry > PageRegistryServiceFileImpl


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 JavaDoc;
23 import java.io.FileReader JavaDoc;
24 import java.util.HashMap JavaDoc;
25
26 import javax.servlet.ServletConfig JavaDoc;
27 import javax.servlet.ServletContext JavaDoc;
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 JavaDoc CONFIG_FILE = "WEB-INF/data/pageregistry.xml";
51     public final static String JavaDoc DEFAULT_MAPPING = "WEB-INF/data/xml/pageregistrymapping.xml";
52     // configuration keys
53
private final static String JavaDoc CONFIG_MAPPING = "mapping.configfile";
54
55     // Castor mapping file
56
private Mapping mapping = null;
57     // Servlet Context
58
private ServletContext JavaDoc servletContext = null;
59     // Root element
60
private PortalImpl registry = null;
61     private RootFragment root = null;
62     private Logger log = null;
63
64     private HashMap JavaDoc fragments = new HashMap JavaDoc();
65
66     public void init (ServletConfig JavaDoc config, Properties properties) throws Exception JavaDoc
67     {
68         servletContext = config.getServletContext();
69         log = Log.getService().getLogger(getClass());
70
71         String JavaDoc _mapping = properties.getString(CONFIG_MAPPING, DEFAULT_MAPPING);
72         File JavaDoc f = new File JavaDoc(_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 JavaDoc 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 JavaDoc config) throws Exception JavaDoc {
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 JavaDoc id) {
113         return (Fragment)fragments.get(id);
114     }
115
116     public void addFragment(Fragment fragment) throws Exception JavaDoc {
117         String JavaDoc id = fragment.getId();
118         if(! fragments.containsKey(id)) {
119             
120             fragments.put(id, fragment);
121
122         } else {
123
124             String JavaDoc msg = "Fragment with this name "+id+" already exists in the pageregistry.xml.";
125             log.error(msg);
126             throw new Exception JavaDoc(msg);
127         }
128
129     }
130
131     private void load() throws Exception JavaDoc
132     {
133         String JavaDoc filename = CONFIG_FILE;
134         
135         File JavaDoc f = new File JavaDoc(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 JavaDoc(filename));
142     }
143
144 }
145
Popular Tags