1 18 19 20 package org.apache.struts.tiles.xmlDefinition; 21 22 import java.util.HashMap ; 23 import java.util.Iterator ; 24 import java.util.Map ; 25 26 import javax.servlet.ServletContext ; 27 import javax.servlet.ServletRequest ; 28 29 import org.apache.struts.tiles.ComponentDefinition; 30 import org.apache.struts.tiles.ComponentDefinitionsFactory; 31 import org.apache.struts.tiles.DefinitionsFactoryException; 32 import org.apache.struts.tiles.FactoryNotFoundException; 33 import org.apache.struts.tiles.NoSuchDefinitionException; 34 35 41 public abstract class FactorySet implements ComponentDefinitionsFactory 42 { 43 44 45 protected Map factories=null; 46 47 54 abstract protected Object getDefinitionsFactoryKey(String name, ServletRequest request, ServletContext servletContext); 55 56 60 abstract protected DefinitionsFactory getDefaultFactory(); 61 62 72 protected DefinitionsFactory getFactory(Object key, ServletRequest request, ServletContext servletContext) 73 throws DefinitionsFactoryException 74 { 75 if(key == null ) 76 return getDefaultFactory(); 77 78 Object factory = factories.get( key ); 79 if( factory == null ) 80 { 81 synchronized(factories) 85 { 86 factory = factories.get( key ); 87 if( factory == null ) 88 { 89 factory = createFactory( key, request, servletContext); 90 factories.put( key, factory ); 91 } } } return (DefinitionsFactory)factory; 95 } 96 97 106 public ComponentDefinition getDefinition(String name, ServletRequest request, ServletContext servletContext) 107 throws NoSuchDefinitionException, DefinitionsFactoryException 108 { 109 if( factories == null ) 110 throw new FactoryNotFoundException( "No definitions factory defined" ); 111 112 Object key = getDefinitionsFactoryKey( name, request, servletContext); 113 DefinitionsFactory factory = getFactory( key, request, servletContext); 114 return factory.getDefinition( name, request, servletContext ); 115 } 116 117 127 abstract protected DefinitionsFactory createFactory(Object key, ServletRequest request, ServletContext servletContext) 128 throws DefinitionsFactoryException; 129 130 135 abstract public void initFactory(ServletContext servletContext, Map properties) 136 throws DefinitionsFactoryException; 137 138 141 public FactorySet() 142 { 143 factories = new HashMap (); 144 } 145 146 150 public String toString() 151 { 152 Iterator i = factories.values().iterator(); 153 StringBuffer buff = new StringBuffer ( "all FactorySet's factory : \n" ); 154 while( i.hasNext() ) 155 { 156 buff.append( i.next().toString() ).append("\n"); 157 } 158 return buff.toString(); 159 } 160 161 } 162 | Popular Tags |