diff --git a/framework/entity/src/main/java/org/apache/ofbiz/entity/util/EntityClassLoader.java b/framework/entity/src/main/java/org/apache/ofbiz/entity/util/EntityClassLoader.java deleted file mode 100644 index 2751c069bd..0000000000 --- a/framework/entity/src/main/java/org/apache/ofbiz/entity/util/EntityClassLoader.java +++ /dev/null @@ -1,212 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.ofbiz.entity.util; - -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.URLConnection; -import java.net.URLStreamHandler; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Enumeration; -import java.util.List; - -import org.apache.ofbiz.base.util.Assert; -import org.apache.ofbiz.base.util.cache.UtilCache; -import org.apache.ofbiz.entity.Delegator; -import org.apache.ofbiz.entity.DelegatorFactory; -import org.apache.ofbiz.entity.GenericValue; -import org.apache.ofbiz.entity.condition.EntityCondition; -import org.apache.ofbiz.entity.condition.EntityOperator; - -/** - * A class loader that retrieves Java resources from the - * JavaResource entity. The entity is searched for the - * resource(s), and if it is not found, searching is delegated - * to the parent class loader. - * - */ -public final class EntityClassLoader extends ClassLoader { - - private static final ThreadLocal inFind = new ThreadLocal(); // Guards against infinite recursion - private static final URLStreamHandler streamHandler = new EntityURLStreamHandler(); - private static final UtilCache misses = UtilCache.createUtilCache("entity.classloader.misses", 500, 0, true); - - public static ClassLoader getInstance(String delegatorName, ClassLoader parent) { - Assert.notNull("delegatorName", delegatorName, "parent", parent); - if (parent instanceof EntityClassLoader) { - EntityClassLoader ecl = (EntityClassLoader) parent; - if (delegatorName.equals(ecl.delegatorName)) { - return ecl; - } - return new EntityClassLoader(delegatorName, ecl.getParent()); - } - return new EntityClassLoader(delegatorName, parent); - } - - private final String delegatorName; - - private EntityClassLoader(String delegatorName, ClassLoader parent) { - super(parent); - this.delegatorName = delegatorName; - } - - @Override - protected URL findResource(String name) { - URL url = null; - if (!isInFind()) { - String key = delegatorName.concat(":").concat(name); - if (misses.containsKey(key)) { - return null; - } - try { - inFind.set(Boolean.TRUE); - Delegator delegator = DelegatorFactory.getDelegator(delegatorName); - GenericValue resourceValue = delegator.findOne("JavaResource", true, "resourceName", name); - if (resourceValue != null) { - url = newUrl(resourceValue); - } else { - misses.put(key, key); - } - } catch (Exception e) { - throw new EntityClassLoaderException(e); - } finally { - inFind.set(Boolean.FALSE); - } - } - return url; - } - - @Override - protected Enumeration findResources(String name) throws IOException { - Enumeration urlEnum = null; - if (!isInFind()) { - String key = delegatorName.concat(":").concat(name); - if (misses.containsKey(key)) { - return null; - } - try { - inFind.set(Boolean.TRUE); - Delegator delegator = DelegatorFactory.getDelegator(delegatorName); - EntityCondition condition = EntityCondition.makeCondition("resourceName", EntityOperator.LIKE, name); - List resourceValues = delegator.findList("JavaResource", condition, null, null, null, true); - if (!resourceValues.isEmpty()) { - List urls = new ArrayList(resourceValues.size()); - for (GenericValue resourceValue : resourceValues) { - urls.add(newUrl(resourceValue)); - } - urlEnum = Collections.enumeration(urls); - } else { - misses.put(key, key); - } - } catch (Exception e) { - throw new EntityClassLoaderException(e); - } finally { - inFind.set(Boolean.FALSE); - } - } - return urlEnum; - } - - public String getDelegatorName() { - return delegatorName; - } - - @Override - public URL getResource(String name) { - Assert.notEmpty("name", name); - URL url = findResource(name); - if (url == null) { - url = getParent().getResource(name); - } - return url; - } - - @Override - public Enumeration getResources(String name) throws IOException { - Assert.notEmpty("name", name); - Enumeration urlEnum = findResources(name); - if (urlEnum == null) { - urlEnum = getParent().getResources(name); - } - return urlEnum; - } - - private boolean isInFind() { - Boolean inFindValue = inFind.get(); - if (inFindValue == null) { - inFindValue = Boolean.FALSE; - inFind.set(inFindValue); - } - return inFindValue; - } - - private URL newUrl(GenericValue resourceValue) throws MalformedURLException { - return new URL("entity", resourceValue.getDelegator().getDelegatorName(), -1, "/".concat(resourceValue - .getString("resourceName")), streamHandler); - } - - private static class EntityURLStreamHandler extends URLStreamHandler { - - @Override - protected URLConnection openConnection(URL url) throws IOException { - Assert.notNull("url", url); - try { - Delegator delegator = DelegatorFactory.getDelegator(url.getHost()); - String resourceName = url.getFile(); - if (resourceName.startsWith("/")) { - resourceName = resourceName.substring(1); - } - GenericValue resourceValue = delegator.findOne("JavaResource", true, "resourceName", resourceName); - return new EntityURLConnection(url, resourceValue.getBytes("resourceValue")); - } catch (Exception e) { - throw new EntityClassLoaderException(e); - } - } - } - - private static class EntityURLConnection extends URLConnection { - - private final byte[] data; - - private EntityURLConnection(URL url, byte[] data) { - super(url); - this.data = data; - } - - @Override - public InputStream getInputStream() throws IOException { - return new ByteArrayInputStream(data); - } - - @Override - public void connect() throws IOException { - } - } - - @SuppressWarnings("serial") - public static class EntityClassLoaderException extends RuntimeException { - public EntityClassLoaderException(Throwable cause) { - super(cause); - } - } -} diff --git a/framework/webapp/src/main/java/org/apache/ofbiz/webapp/control/ControlServlet.java b/framework/webapp/src/main/java/org/apache/ofbiz/webapp/control/ControlServlet.java index cf3c30d0c1..35c58c9237 100644 --- a/framework/webapp/src/main/java/org/apache/ofbiz/webapp/control/ControlServlet.java +++ b/framework/webapp/src/main/java/org/apache/ofbiz/webapp/control/ControlServlet.java @@ -47,6 +47,7 @@ import org.apache.ofbiz.security.Security; import org.apache.ofbiz.service.LocalDispatcher; import org.apache.ofbiz.webapp.stats.ServerHitBin; import org.apache.ofbiz.webapp.stats.VisitHandler; + import freemarker.ext.servlet.ServletContextHashModel; /** @@ -157,10 +158,6 @@ public class ControlServlet extends HttpServlet { request.setAttribute("delegator", delegator); // always put this in the session too so that session events can use the delegator session.setAttribute("delegatorName", delegator.getDelegatorName()); - /* Uncomment this to enable the EntityClassLoader - ClassLoader loader = EntityClassLoader.getInstance(delegator.getDelegatorName(), Thread.currentThread().getContextClassLoader()); - Thread.currentThread().setContextClassLoader(loader); - */ } LocalDispatcher dispatcher = (LocalDispatcher) session.getAttribute("dispatcher");