1 package org.objectweb.celtix.js.rhino; 2 3 import java.io.BufferedReader ; 4 import java.io.File ; 5 import java.io.FileReader ; 6 7 import java.util.logging.Level ; 8 import java.util.logging.Logger ; 9 10 import javax.xml.ws.Service; 11 12 import org.mozilla.javascript.Context; 13 import org.mozilla.javascript.ContextFactory; 14 import org.mozilla.javascript.Script; 15 import org.mozilla.javascript.Scriptable; 16 17 public class ProviderFactory { 18 public static final String ILLEGAL_SVCMD_MODE = ": unknown ServiceMode: "; 19 public static final String ILLEGAL_SVCMD_TYPE = ": ServiceMode value must be of type string"; 20 public static final String NO_SUCH_FILE = ": file does not exist"; 21 public static final String NO_PROVIDER = ": file contains no WebServiceProviders"; 22 23 private static final Logger LOG = Logger.getLogger(ProviderFactory.class.getName()); 24 25 private String epAddress; 26 private boolean isBaseAddr; 27 28 static { 29 ContextFactory.initGlobal(new RhinoContextFactory()); 30 } 31 32 public ProviderFactory(String baseAddr) { 33 epAddress = baseAddr; 34 isBaseAddr = true; 35 } 36 37 public ProviderFactory() { 38 } 40 41 public void createAndPublish(File f, String epAddr, boolean isBase) throws Exception { 42 publishImpl(f, epAddr, isBase); 43 } 44 45 public synchronized void createAndPublish(File f) throws Exception { 46 publishImpl(f, epAddress, isBaseAddr); 47 } 48 49 private void publishImpl(File f, String epAddr, boolean isBase) throws Exception { 50 if (!f.exists()) { 51 throw new Exception (f.getPath() + NO_SUCH_FILE); 52 } 53 boolean isE4X = f.getName().endsWith(".jsx"); 54 BufferedReader bufrd = new BufferedReader (new FileReader (f)); 55 String line = null; 56 StringBuffer sb = new StringBuffer (); 57 for (;;) { 58 line = bufrd.readLine(); 59 if (line == null) { 60 break; 61 } 62 sb.append(line).append("\n"); 63 } 64 String scriptStr = sb.toString(); 65 66 Context cx = Context.enter(); 67 boolean providerFound = false; 68 try { 69 Scriptable scriptScope = cx.initStandardObjects(null, true); 70 Object [] ids = compileScript(cx, scriptStr, scriptScope, f); 71 if (ids.length > 0) { 72 Service.Mode mode = Service.Mode.PAYLOAD; 73 for (Object idObj : ids) { 74 if (!(idObj instanceof String )) { 75 continue; 76 } 77 String id = (String )idObj; 78 if (!id.startsWith("WebServiceProvider")) { 79 continue; 80 } 81 Object obj = scriptScope.get(id, scriptScope); 82 if (!(obj instanceof Scriptable)) { 83 continue; 84 } 85 Scriptable wspVar = (Scriptable)obj; 86 providerFound = true; 87 obj = wspVar.get("ServiceMode", wspVar); 88 if (obj != Scriptable.NOT_FOUND) { 89 if (obj instanceof String ) { 90 String value = (String )obj; 91 if ("PAYLOAD".equalsIgnoreCase(value)) { 92 mode = Service.Mode.PAYLOAD; 93 } else if ("MESSAGE".equalsIgnoreCase(value)) { 94 mode = Service.Mode.MESSAGE; 95 } else { 96 throw new Exception (f.getPath() + ILLEGAL_SVCMD_MODE + value); 97 } 98 } else { 99 throw new Exception (f.getPath() + ILLEGAL_SVCMD_TYPE); 100 } 101 } 102 AbstractDOMProvider provider 103 = createProvider(mode, scriptScope, wspVar, 104 epAddr, isBase, isE4X); 105 try { 106 provider.publish(); 107 } catch (AbstractDOMProvider.JSDOMProviderException ex) { 108 StringBuffer msg = new StringBuffer (f.getPath()); 109 msg.append(": ").append(ex.getMessage()); 110 throw new Exception (msg.toString()); 111 } 112 } 113 } 114 } finally { 115 Context.exit(); 116 } 117 if (!providerFound) { 118 throw new Exception (f.getPath() + NO_PROVIDER); 119 } 120 } 121 122 protected AbstractDOMProvider createProvider(Service.Mode mode, Scriptable scope, 123 Scriptable wsp, String epAddr, 124 boolean isBase, boolean e4x) throws Exception { 125 if (LOG.isLoggable(Level.FINE)) { 126 String modestr = (mode == Service.Mode.PAYLOAD) ? "payload" : "message"; 127 String type = e4x ? "E4X" : "JavaScript"; 128 String base = isBase ? "base " : ""; 129 StringBuffer msg = new StringBuffer ("creating a "); 130 msg.append(modestr) 131 .append(" ") 132 .append(type) 133 .append(" provider for ") 134 .append(base) 135 .append("address ") 136 .append(epAddr); 137 LOG.log(Level.FINE, msg.toString()); 138 } 139 AbstractDOMProvider provider = null; 140 if (mode == Service.Mode.PAYLOAD) { 141 provider = new DOMPayloadProvider(scope, wsp, epAddr, isBase, e4x); 142 } else if (mode == Service.Mode.MESSAGE) { 143 provider = new DOMMessageProvider(scope, wsp, epAddr, isBase, e4x); 144 } 145 return provider; 146 } 147 148 private Object [] compileScript(Context cx, String scriptStr, Scriptable scriptScope, File f) { 149 int opt = cx.getOptimizationLevel(); 150 cx.setOptimizationLevel(-1); 151 Script script = cx.compileString(scriptStr, f.getName(), 1, null); 152 script.exec(cx, scriptScope); 153 Object [] ids = scriptScope.getIds(); 154 cx.setOptimizationLevel(opt); 155 script = cx.compileString(scriptStr, f.getName(), 1, null); 156 script.exec(cx, scriptScope); 157 return ids; 158 } 159 160 static class RhinoContextFactory extends ContextFactory { 161 public boolean hasFeature(Context cx, int feature) { 162 if (feature == Context.FEATURE_DYNAMIC_SCOPE) { 163 return true; 164 } 165 return super.hasFeature(cx, feature); 166 } 167 } 168 } 169 | Popular Tags |