1 package org.bsf.smartValueObject.mediator; 2 3 import org.bsf.smartValueObject.SmartAccess; 4 import org.apache.commons.beanutils.BeanUtils; 5 import org.apache.commons.logging.Log; 6 import org.apache.commons.logging.LogFactory; 7 8 import javax.naming.Context ; 9 import javax.naming.InitialContext ; 10 import javax.naming.NamingException ; 11 import java.lang.reflect.Field ; 12 import java.util.Map ; 13 import java.util.HashMap ; 14 import java.util.Properties ; 15 16 23 public class EjbSvoMediator implements Mediator { 24 private static Log log = LogFactory.getLog(EjbSvoMediator.class); 25 26 private Class clazz; 27 28 private Map config; 29 30 private String indexField; 31 32 private Map objects; 33 34 private Context context = null; 35 36 private Class homeClass; 37 38 public static final String CONTEXT = "context"; 39 public static final String HOMECLASS = "homeclass"; 40 41 47 public EjbSvoMediator(Class clazz, Map config) { 48 this.clazz = clazz; 49 this.config = config; 50 this.objects = new HashMap (); 51 readConfig(this.config); 52 } 53 54 61 public Object getGraph(Object prototype) throws MediatorException { 62 log.info("getGraph(" + prototype + ")"); 63 if (prototype == null || prototype.getClass().getName() != clazz.getName()) 64 throw new MediatorException("Unknown prototype"); 65 66 return lookFor(prototype); 67 } 68 69 74 public ChangeSummary updateGraph(Object graph) throws MediatorException { 75 log.info("updateGraph(" + graph + ")"); 76 if (graph == null || graph.getClass().getName() != clazz.getName()) 77 throw new MediatorException("Unknown prototype"); 78 79 if (!SmartAccess.isVersionable(graph)) { 80 throw new MediatorException("Object not versionable"); 81 } 82 83 if (SmartAccess.isGraphDirty(graph)) { 84 storeEJB(graph); 85 } 86 return new ChangeSummary(null, null); 87 } 88 89 94 public void deleteGraph(Object graph) 95 throws MediatorException { 96 } 97 98 private void storeEJB(Object graph) throws MediatorException { 99 log.info("storeEJB(" + graph + ")"); 100 101 Object index; 102 try { 103 index = graph.getClass().getField(indexField).get(graph); 104 } catch (Exception e) { 105 throw new MediatorException(e); 106 } 107 108 objects.put(index, graph); 109 } 110 111 private Object lookFor(Object o) throws MediatorException { 112 Field [] fields = o.getClass().getDeclaredFields(); 113 for (int i = 0; i < fields.length; i++) { 114 Field field = fields[i]; 115 if (field.getName().equals(indexField)) { 116 Object index; 117 try { 118 index = field.get(o); 119 } catch (Exception e) { 120 throw new MediatorException(e); 121 } 122 123 if (index != null) 124 return lookForEJB(index); 125 } 126 } 127 128 throw new MediatorException("Object not found"); 129 } 130 131 private Object lookForEJB(Object index) throws MediatorException { 132 log.info("lookForEJB: " + index); 133 Object o = createNewVO(); 134 Object src = lookForIndex(index); 135 136 try { 137 BeanUtils.copyProperties(o, src); 138 } catch (Exception e) { 139 throw new MediatorException(e); 140 } 141 return o; 142 } 143 144 private Object lookForIndex(Object index) { 146 return objects.get(index); 147 } 148 149 private void readConfig(Map config) { 150 indexField = (String ) config.get(INDEXFIELD); 151 context = (Context ) config.get(CONTEXT); 152 } 153 154 159 private Object createNewVO() throws MediatorException { 160 Object o; 161 try { 162 o = clazz.newInstance(); 163 } catch (Exception e) { 164 throw new MediatorException(e); 165 } 166 return o; 167 } 168 169 170 private Context getContext() { 171 if (context == null) { 172 try { 173 Properties props = new Properties (); 174 props.put(Context.INITIAL_CONTEXT_FACTORY, 175 "org.jnp.interfaces.NamingContextFactory"); 176 props.put(Context.PROVIDER_URL, 177 "jnp://localhost:1099"); 178 props.put(Context.URL_PKG_PREFIXES, 179 "org.jboss.naming:org.jnp.interfaces"); 180 181 context = new InitialContext (props); 182 } catch (NamingException e) { 183 throw new RuntimeException (e.getLocalizedMessage()); 184 } 185 } 186 187 return context; 188 } 189 } 190 191 | Popular Tags |