1 21 22 package com.sun.enterprise.tools.guiframework.view; 23 24 import com.iplanet.jato.RequestManager; 25 26 import com.sun.enterprise.tools.guiframework.exception.FrameworkException; 27 import com.sun.enterprise.tools.guiframework.view.descriptors.ViewDescriptor; 28 29 import java.io.IOException ; 30 import java.io.ObjectInputStream ; 31 import java.io.ObjectOutputStream ; 32 import java.net.URL ; 33 import java.net.MalformedURLException ; 34 import java.util.HashMap ; 35 import java.util.Map ; 36 37 import javax.servlet.ServletContext ; 38 39 import org.xml.sax.EntityResolver ; 40 41 45 public class ViewDescriptorManager implements ViewDescriptorManagerInterface { 46 47 50 private ViewDescriptorManager() { 51 } 52 53 54 57 public static ViewDescriptorManager getInstance() { 58 return _instance; 59 } 60 61 62 68 public void setDTDURLBase(String base) { 69 _dtdBase = base; 70 } 71 72 73 77 public String getDTDURLBase() { 78 if (_dtdBase == null) { 80 ServletContext servletContext = 81 RequestManager.getRequestContext().getServletContext(); 82 String path = "file:///"+servletContext.getRealPath("/")+"xml/"; 83 setDTDURLBase(path); 84 } 85 return _dtdBase; 86 } 87 88 89 92 public void setViewDescriptorURL(String url) { 93 try { 94 setViewDescriptorURL(new URL (url)); 95 } catch (MalformedURLException ex) { 96 throw new FrameworkException(ex); 97 } 98 } 99 100 101 104 public void setViewDescriptorURL(URL url) { 105 _descURL = url; 106 } 107 108 109 112 public URL getViewDescriptorURL() { 113 return _descURL; 114 } 115 116 117 public void setViewXMLEntityResolver(EntityResolver entityResolver) { 118 _viewXMLEntityResolver = entityResolver; 119 } 120 121 public EntityResolver getViewXMLEntityResolver() { 122 return _viewXMLEntityResolver; 123 } 124 125 129 public void clearCache() { 130 _clearCache = true; 131 } 132 133 134 public ViewDescriptor getViewDescriptor(String name) { 135 ViewDescriptor viewDesc = null; 136 if (_clearCache) { 137 synchronized (this) { 139 if (_clearCache) { 140 _descriptors = new HashMap (); 141 _clearCache = false; 142 _processAllDescriptors = true; 143 } 144 } 145 } else { 146 viewDesc = (ViewDescriptor) _descriptors.get(name); 148 if (viewDesc != null) { 149 return viewDesc; 150 } 151 } 152 153 try { 155 156 URL url = getViewDescriptorURL(); 158 if (url == null) { 159 throw new FrameworkException( 160 "View Descriptor XML file not specified!!!"); 161 } 162 163 ViewXMLReader reader = null; 165 try { 166 reader = new ViewXMLReader( 169 url.openStream(), getDTDURLBase(), getJSPRoot(), 170 getViewXMLEntityResolver()); 171 } catch (IOException ex) { 172 throw new FrameworkException("Unable to open URL: '"+ 173 url+"'.", ex); 174 } catch (Exception ex) { 175 throw new FrameworkException(ex); 176 } 177 178 if (_processAllDescriptors == true) { 179 synchronized(this) { 180 if (_descriptors.size() == 0) { 181 _descriptors = reader.getAllViewDescriptors(); 182 } 183 _processAllDescriptors = false; 184 } 185 viewDesc = (ViewDescriptor)_descriptors.get(name); 186 } 187 188 if (viewDesc == null) { 189 viewDesc = reader.getViewDescriptor(name); 191 synchronized (this) { 193 _descriptors.put(name, viewDesc); 194 } 195 } 196 197 return viewDesc; 199 } catch (Exception ex) { 200 throw new FrameworkException("Unable to locate ViewDescriptor '"+ 201 name+"'.", ex); 202 } 203 } 204 205 208 public void deserialize(ObjectInputStream stream) throws IOException , ClassNotFoundException { 209 _descriptors = (Map ) stream.readObject(); 210 } 211 212 215 public void serialize(ObjectOutputStream stream) throws IOException { 216 stream.writeObject(_descriptors); 217 } 218 219 220 223 public void setJSPRoot(String root) { 224 _jspRoot = root; 225 } 226 227 228 231 public String getJSPRoot() { 232 return _jspRoot; 233 } 234 235 236 239 private static ViewDescriptorManager _instance = 240 new ViewDescriptorManager(); 241 242 243 246 protected String _jspRoot = ""; 247 248 249 252 protected boolean _clearCache = false; 253 254 257 protected Map _descriptors = new HashMap (); 258 259 260 263 protected URL _descURL = null; 264 265 266 269 protected String _dtdBase = null; 270 271 protected EntityResolver _viewXMLEntityResolver = null; 272 273 276 protected boolean _processAllDescriptors = true; 277 278 } 279 | Popular Tags |