Esempio n. 1
0
  public static boolean initialized() {
    if (LOG != null) {
      return true;
    }

    synchronized (Log.class) {
      if (__initialized) {
        return LOG != null;
      }
      __initialized = true;
    }

    try {
      Class<?> log_class = Loader.loadClass(Log.class, __logClass);
      if (LOG == null || !LOG.getClass().equals(log_class)) {
        LOG = (Logger) log_class.newInstance();
        LOG.debug("Logging to {} via {}", LOG, log_class.getName());
      }
    } catch (Throwable e) {
      // Unable to load specified Logger implementation, default to standard logging.
      initStandardLogging(e);
    }

    return LOG != null;
  }
Esempio n. 2
0
 {
   try {
     Class<?> e = Loader.loadClass(getClass(), "java.lang.Enum");
     _valueOf = e.getMethod("valueOf", Class.class, String.class);
   } catch (Exception e) {
     throw new RuntimeException("!Enums", e);
   }
 }
Esempio n. 3
0
  /**
   * Find a classpath resource. The {@link java.lang.Class#getResource(String)} method is used to
   * lookup the resource. If it is not found, then the {@link Loader#getResource(Class, String)}
   * method is used. If it is still not found, then {@link ClassLoader#getSystemResource(String)} is
   * used. Unlike {@link ClassLoader#getSystemResource(String)} this method does not check for
   * normal resources.
   *
   * @param name The relative name of the resource
   * @param useCaches True if URL caches are to be used.
   * @param checkParents True if forced searching of parent Classloaders is performed to work around
   *     loaders with inverted priorities
   * @return Resource or null
   */
  public static Resource newClassPathResource(
      String name, boolean useCaches, boolean checkParents) {
    URL url = Resource.class.getResource(name);

    if (url == null) url = Loader.getResource(Resource.class, name);
    if (url == null) return null;
    return newResource(url, useCaches);
  }
Esempio n. 4
0
 public Object fromJSON(Map map) {
   if (!_fromJSON) throw new UnsupportedOperationException();
   try {
     Class c = Loader.loadClass(getClass(), (String) map.get("class"));
     return _valueOf.invoke(null, c, map.get("value"));
   } catch (Exception e) {
     LOG.warn(e);
   }
   return null;
 }
Esempio n. 5
0
  /**
   * Create MBean for Object. Attempts to create an MBean for the object by searching the package
   * and class name space. For example an object of the type
   *
   * <PRE>
   * class com.acme.MyClass extends com.acme.util.BaseClass implements com.acme.Iface
   * </PRE>
   *
   * Then this method would look for the following classes:
   *
   * <UL>
   *   <LI>com.acme.jmx.MyClassMBean
   *   <LI>com.acme.util.jmx.BaseClassMBean
   *   <LI>org.eclipse.jetty.jmx.ObjectMBean
   * </UL>
   *
   * @param o The object
   * @return A new instance of an MBean for the object or null.
   */
  public static Object mbeanFor(Object o) {
    try {
      Class oClass = o.getClass();
      Object mbean = null;

      while (mbean == null && oClass != null) {
        String pName = oClass.getPackage().getName();
        String cName = oClass.getName().substring(pName.length() + 1);
        String mName = pName + ".jmx." + cName + "MBean";

        try {
          Class mClass =
              (Object.class.equals(oClass))
                  ? oClass = ObjectMBean.class
                  : Loader.loadClass(oClass, mName, true);
          if (LOG.isDebugEnabled()) LOG.debug("mbeanFor " + o + " mClass=" + mClass);

          try {
            Constructor constructor = mClass.getConstructor(OBJ_ARG);
            mbean = constructor.newInstance(new Object[] {o});
          } catch (Exception e) {
            LOG.ignore(e);
            if (ModelMBean.class.isAssignableFrom(mClass)) {
              mbean = mClass.newInstance();
              ((ModelMBean) mbean).setManagedResource(o, "objectReference");
            }
          }

          if (LOG.isDebugEnabled()) LOG.debug("mbeanFor " + o + " is " + mbean);
          return mbean;
        } catch (ClassNotFoundException e) {
          // The code below was modified to fix bugs 332200 and JETTY-1416
          // The issue was caused by additional information added to the
          // message after the class name when running in Apache Felix,
          // as well as before the class name when running in JBoss.
          if (e.getMessage().contains(mName)) LOG.ignore(e);
          else LOG.warn(e);
        } catch (Error e) {
          LOG.warn(e);
          mbean = null;
        } catch (Exception e) {
          LOG.warn(e);
          mbean = null;
        }

        oClass = oClass.getSuperclass();
      }
    } catch (Exception e) {
      LOG.ignore(e);
    }
    return null;
  }
 /**
  * Parse the given classes
  *
  * @param classNames
  * @param resolver
  * @throws Exception
  */
 public void parse(List<String> classNames, ClassNameResolver resolver) throws Exception {
   for (String s : classNames) {
     if ((resolver == null)
         || (!resolver.isExcluded(s) && (!isParsed(s) || resolver.shouldOverride(s)))) {
       s = s.replace('.', '/') + ".class";
       URL resource = Loader.getResource(this.getClass(), s, false);
       if (resource != null) {
         Resource r = Resource.newResource(resource);
         scanClass(r.getInputStream());
       }
     }
   }
 }
  /**
   * Parse a given class
   *
   * @param className
   * @param resolver
   * @throws Exception
   */
  public void parse(String className, ClassNameResolver resolver) throws Exception {
    if (className == null) return;

    if (!resolver.isExcluded(className)) {
      if (!isParsed(className) || resolver.shouldOverride(className)) {
        className = className.replace('.', '/') + ".class";
        URL resource = Loader.getResource(this.getClass(), className, false);
        if (resource != null) {
          Resource r = Resource.newResource(resource);
          scanClass(r.getInputStream());
        }
      }
    }
  }
Esempio n. 8
0
  /* ------------------------------------------------------------ */
  @SuppressWarnings("unchecked")
  @Override
  public void doStart() throws Exception {
    // if no class already loaded and no classname, make servlet permanently unavailable
    if (_class == null && (_className == null || _className.equals("")))
      throw new UnavailableException("No class for Servlet or Filter for " + _name);

    // try to load class
    if (_class == null) {
      try {
        _class = Loader.loadClass(Holder.class, _className);
        if (LOG.isDebugEnabled()) LOG.debug("Holding {}", _class);
      } catch (Exception e) {
        LOG.warn(e);
        throw new UnavailableException(e.getMessage());
      }
    }
  }
 /**
  * Parse the given class, optionally walking its inheritance hierarchy
  *
  * @param clazz
  * @param resolver
  * @param visitSuperClasses
  * @throws Exception
  */
 public void parse(Class<?> clazz, ClassNameResolver resolver, boolean visitSuperClasses)
     throws Exception {
   Class<?> cz = clazz;
   while (cz != null) {
     if (!resolver.isExcluded(cz.getName())) {
       if (!isParsed(cz.getName()) || resolver.shouldOverride(cz.getName())) {
         String nameAsResource = cz.getName().replace('.', '/') + ".class";
         URL resource = Loader.getResource(this.getClass(), nameAsResource, false);
         if (resource != null) {
           Resource r = Resource.newResource(resource);
           scanClass(r.getInputStream());
         }
       }
     }
     if (visitSuperClasses) cz = cz.getSuperclass();
     else cz = null;
   }
 }
Esempio n. 10
0
  public void contextDestroyed(ServletContextEvent sce) {
    try {
      // Check that the BeanELResolver class is on the classpath
      Class beanELResolver = Loader.loadClass(this.getClass(), "javax.el.BeanELResolver");

      // Get a reference via reflection to the properties field which is holding class references
      Field field = getField(beanELResolver);

      // Get rid of references
      purgeEntries(field);

      LOG.info("javax.el.BeanELResolver purged");
    } catch (ClassNotFoundException e) {
      // BeanELResolver not on classpath, ignore
    } catch (SecurityException e) {
      LOG.warn("Cannot purge classes from javax.el.BeanELResolver", e);
    } catch (IllegalArgumentException e) {
      LOG.warn("Cannot purge classes from javax.el.BeanELResolver", e);
    } catch (IllegalAccessException e) {
      LOG.warn("Cannot purge classes from javax.el.BeanELResolver", e);
    } catch (NoSuchFieldException e) {
      LOG.warn("Cannot purge classes from javax.el.BeanELResolver", e);
    }
  }
Esempio n. 11
0
  /** @see org.eclipse.jetty.security.MappedLoginService#doStart() */
  @Override
  protected void doStart() throws Exception {
    Properties properties = new Properties();
    Resource resource = Resource.newResource(_config);
    properties.load(resource.getInputStream());

    _jdbcDriver = properties.getProperty("jdbcdriver");
    _url = properties.getProperty("url");
    _userName = properties.getProperty("username");
    _password = properties.getProperty("password");
    String _userTable = properties.getProperty("usertable");
    _userTableKey = properties.getProperty("usertablekey");
    String _userTableUserField = properties.getProperty("usertableuserfield");
    _userTablePasswordField = properties.getProperty("usertablepasswordfield");
    String _roleTable = properties.getProperty("roletable");
    String _roleTableKey = properties.getProperty("roletablekey");
    _roleTableRoleField = properties.getProperty("roletablerolefield");
    String _userRoleTable = properties.getProperty("userroletable");
    String _userRoleTableUserKey = properties.getProperty("userroletableuserkey");
    String _userRoleTableRoleKey = properties.getProperty("userroletablerolekey");
    _cacheTime = new Integer(properties.getProperty("cachetime"));

    if (_jdbcDriver == null
        || _jdbcDriver.equals("")
        || _url == null
        || _url.equals("")
        || _userName == null
        || _userName.equals("")
        || _password == null
        || _cacheTime < 0) {
      LOG.warn("UserRealm " + getName() + " has not been properly configured");
    }
    _cacheTime *= 1000;
    _lastHashPurge = 0;
    _userSql =
        "select "
            + _userTableKey
            + ","
            + _userTablePasswordField
            + " from "
            + _userTable
            + " where "
            + _userTableUserField
            + " = ?";
    _roleSql =
        "select r."
            + _roleTableRoleField
            + " from "
            + _roleTable
            + " r, "
            + _userRoleTable
            + " u where u."
            + _userRoleTableUserKey
            + " = ?"
            + " and r."
            + _roleTableKey
            + " = u."
            + _userRoleTableRoleKey;

    Loader.loadClass(this.getClass(), _jdbcDriver).newInstance();
    super.doStart();
  }
Esempio n. 12
0
  /* ------------------------------------------------------------ */
  public UserIdentity login(final String username, final Object credentials) {
    try {
      CallbackHandler callbackHandler = null;

      if (_callbackHandlerClass == null) {
        callbackHandler =
            new CallbackHandler() {
              public void handle(Callback[] callbacks)
                  throws IOException, UnsupportedCallbackException {
                for (Callback callback : callbacks) {
                  if (callback instanceof NameCallback) {
                    ((NameCallback) callback).setName(username);
                  } else if (callback instanceof PasswordCallback) {
                    ((PasswordCallback) callback)
                        .setPassword((char[]) credentials.toString().toCharArray());
                  } else if (callback instanceof ObjectCallback) {
                    ((ObjectCallback) callback).setObject(credentials);
                  } else if (callback instanceof RequestParameterCallback) {
                    HttpChannel channel = HttpChannel.getCurrentHttpChannel();

                    if (channel == null) return;
                    Request request = channel.getRequest();

                    if (request != null) {
                      RequestParameterCallback rpc = (RequestParameterCallback) callback;
                      rpc.setParameterValues(
                          Arrays.asList(request.getParameterValues(rpc.getParameterName())));
                    }
                  } else throw new UnsupportedCallbackException(callback);
                }
              }
            };
      } else {
        Class clazz = Loader.loadClass(getClass(), _callbackHandlerClass);
        callbackHandler = (CallbackHandler) clazz.newInstance();
      }
      // set up the login context
      // TODO jaspi requires we provide the Configuration parameter
      Subject subject = new Subject();
      LoginContext loginContext = new LoginContext(_loginModuleName, subject, callbackHandler);

      loginContext.login();

      // login success
      JAASUserPrincipal userPrincipal =
          new JAASUserPrincipal(getUserName(callbackHandler), subject, loginContext);
      subject.getPrincipals().add(userPrincipal);

      return _identityService.newUserIdentity(subject, userPrincipal, getGroups(subject));
    } catch (LoginException e) {
      LOG.warn(e);
    } catch (IOException e) {
      LOG.warn(e);
    } catch (UnsupportedCallbackException e) {
      LOG.warn(e);
    } catch (InstantiationException e) {
      LOG.warn(e);
    } catch (IllegalAccessException e) {
      LOG.warn(e);
    } catch (ClassNotFoundException e) {
      LOG.warn(e);
    }
    return null;
  }
Esempio n. 13
0
  public XmlParser newParser() throws ClassNotFoundException {
    XmlParser xmlParser = new XmlParser();
    // set up cache of DTDs and schemas locally
    URL dtd22 = Loader.getResource(Servlet.class, "javax/servlet/resources/web-app_2_2.dtd", true);
    URL dtd23 = Loader.getResource(Servlet.class, "javax/servlet/resources/web-app_2_3.dtd", true);
    URL j2ee14xsd = Loader.getResource(Servlet.class, "javax/servlet/resources/j2ee_1_4.xsd", true);
    URL webapp24xsd =
        Loader.getResource(Servlet.class, "javax/servlet/resources/web-app_2_4.xsd", true);
    URL webapp25xsd =
        Loader.getResource(Servlet.class, "javax/servlet/resources/web-app_2_5.xsd", true);
    URL webapp30xsd =
        Loader.getResource(Servlet.class, "javax/servlet/resources/web-app_3_0.xsd", true);
    URL webcommon30xsd =
        Loader.getResource(Servlet.class, "javax/servlet/resources/web-common_3_0.xsd", true);
    URL webfragment30xsd =
        Loader.getResource(Servlet.class, "javax/servlet/resources/web-fragment_3_0.xsd", true);
    URL schemadtd =
        Loader.getResource(Servlet.class, "javax/servlet/resources/XMLSchema.dtd", true);
    URL xmlxsd = Loader.getResource(Servlet.class, "javax/servlet/resources/xml.xsd", true);
    URL webservice11xsd =
        Loader.getResource(
            Servlet.class, "javax/servlet/resources/j2ee_web_services_client_1_1.xsd", true);
    URL webservice12xsd =
        Loader.getResource(
            Servlet.class, "javax/servlet/resources/javaee_web_services_client_1_2.xsd", true);
    URL datatypesdtd =
        Loader.getResource(Servlet.class, "javax/servlet/resources/datatypes.dtd", true);

    URL jsp20xsd = null;
    URL jsp21xsd = null;

    try {
      Class<?> jsp_page = Loader.loadClass(WebXmlConfiguration.class, "javax.servlet.jsp.JspPage");
      jsp20xsd = jsp_page.getResource("/javax/servlet/resources/jsp_2_0.xsd");
      jsp21xsd = jsp_page.getResource("/javax/servlet/resources/jsp_2_1.xsd");
    } catch (Exception e) {
      LOG.ignore(e);
    } finally {
      if (jsp20xsd == null)
        jsp20xsd = Loader.getResource(Servlet.class, "javax/servlet/resources/jsp_2_0.xsd", true);
      if (jsp21xsd == null)
        jsp21xsd = Loader.getResource(Servlet.class, "javax/servlet/resources/jsp_2_1.xsd", true);
    }

    redirect(xmlParser, "web-app_2_2.dtd", dtd22);
    redirect(xmlParser, "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN", dtd22);
    redirect(xmlParser, "web.dtd", dtd23);
    redirect(xmlParser, "web-app_2_3.dtd", dtd23);
    redirect(xmlParser, "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN", dtd23);
    redirect(xmlParser, "XMLSchema.dtd", schemadtd);
    redirect(xmlParser, "http://www.w3.org/2001/XMLSchema.dtd", schemadtd);
    redirect(xmlParser, "-//W3C//DTD XMLSCHEMA 200102//EN", schemadtd);
    redirect(xmlParser, "jsp_2_0.xsd", jsp20xsd);
    redirect(xmlParser, "http://java.sun.com/xml/ns/j2ee/jsp_2_0.xsd", jsp20xsd);
    redirect(xmlParser, "http://java.sun.com/xml/ns/javaee/jsp_2_1.xsd", jsp21xsd);
    redirect(xmlParser, "j2ee_1_4.xsd", j2ee14xsd);
    redirect(xmlParser, "http://java.sun.com/xml/ns/j2ee/j2ee_1_4.xsd", j2ee14xsd);
    redirect(xmlParser, "web-app_2_4.xsd", webapp24xsd);
    redirect(xmlParser, "http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd", webapp24xsd);
    redirect(xmlParser, "web-app_2_5.xsd", webapp25xsd);
    redirect(xmlParser, "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd", webapp25xsd);
    redirect(xmlParser, "web-app_3_0.xsd", webapp30xsd);
    redirect(xmlParser, "http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd", webapp30xsd);
    redirect(xmlParser, "web-common_3_0.xsd", webcommon30xsd);
    redirect(xmlParser, "http://java.sun.com/xml/ns/javaee/web-common_3_0.xsd", webcommon30xsd);
    redirect(xmlParser, "web-fragment_3_0.xsd", webfragment30xsd);
    redirect(xmlParser, "http://java.sun.com/xml/ns/javaee/web-fragment_3_0.xsd", webfragment30xsd);
    redirect(xmlParser, "xml.xsd", xmlxsd);
    redirect(xmlParser, "http://www.w3.org/2001/xml.xsd", xmlxsd);
    redirect(xmlParser, "datatypes.dtd", datatypesdtd);
    redirect(xmlParser, "http://www.w3.org/2001/datatypes.dtd", datatypesdtd);
    redirect(xmlParser, "j2ee_web_services_client_1_1.xsd", webservice11xsd);
    redirect(
        xmlParser,
        "http://www.ibm.com/webservices/xsd/j2ee_web_services_client_1_1.xsd",
        webservice11xsd);
    redirect(xmlParser, "javaee_web_services_client_1_2.xsd", webservice12xsd);
    redirect(
        xmlParser,
        "http://www.ibm.com/webservices/xsd/javaee_web_services_client_1_2.xsd",
        webservice12xsd);
    return xmlParser;
  }
Esempio n. 14
0
  public MBeanInfo getMBeanInfo() {
    try {
      if (_info == null) {
        // Start with blank lazy lists attributes etc.
        String desc = null;
        Object attributes = null;
        Object constructors = null;
        Object operations = null;
        Object notifications = null;

        // Find list of classes that can influence the mbean
        Class o_class = _managed.getClass();
        Object influences = findInfluences(null, _managed.getClass());

        // Set to record defined items
        Set defined = new HashSet();

        // For each influence
        for (int i = 0; i < LazyList.size(influences); i++) {
          Class oClass = (Class) LazyList.get(influences, i);

          // look for a bundle defining methods
          if (Object.class.equals(oClass)) oClass = ObjectMBean.class;
          String pName = oClass.getPackage().getName();
          String cName = oClass.getName().substring(pName.length() + 1);
          String rName = pName.replace('.', '/') + "/jmx/" + cName + "-mbean";

          try {
            LOG.debug(rName);
            ResourceBundle bundle =
                Loader.getResourceBundle(o_class, rName, true, Locale.getDefault());

            // Extract meta data from bundle
            Enumeration e = bundle.getKeys();
            while (e.hasMoreElements()) {
              String key = (String) e.nextElement();
              String value = bundle.getString(key);

              // Determin if key is for mbean , attribute or for operation
              if (key.equals(cName)) {
                // set the mbean description
                if (desc == null) desc = value;
              } else if (key.indexOf('(') > 0) {
                // define an operation
                if (!defined.contains(key) && key.indexOf('[') < 0) {
                  defined.add(key);
                  operations = LazyList.add(operations, defineOperation(key, value, bundle));
                }
              } else {
                // define an attribute
                if (!defined.contains(key)) {
                  defined.add(key);
                  MBeanAttributeInfo info = defineAttribute(key, value);
                  if (info != null) attributes = LazyList.add(attributes, info);
                }
              }
            }
          } catch (MissingResourceException e) {
            LOG.ignore(e);
          }
        }

        _info =
            new MBeanInfo(
                o_class.getName(),
                desc,
                (MBeanAttributeInfo[]) LazyList.toArray(attributes, MBeanAttributeInfo.class),
                (MBeanConstructorInfo[]) LazyList.toArray(constructors, MBeanConstructorInfo.class),
                (MBeanOperationInfo[]) LazyList.toArray(operations, MBeanOperationInfo.class),
                (MBeanNotificationInfo[])
                    LazyList.toArray(notifications, MBeanNotificationInfo.class));
      }
    } catch (RuntimeException e) {
      LOG.warn(e);
      throw e;
    }
    return _info;
  }