1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
53 private final static String CONFIG_MAPPING = "mapping.configfile";
54
55
56 private Mapping mapping = null;
57
58 private ServletContext servletContext = null;
59
60 private PortalImpl registry = null;
61 private RootFragment root = null;
62 private Logger log = null;
63
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
98 fragments = new HashMap();
99 root = registry.build(config);
100
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 }