Ejemplo n.º 1
0
 // Serialize the bean using the specified namespace prefix & uri
 public String serialize(Object bean) throws IntrospectionException, IllegalAccessException {
   // Use the class name as the name of the root element
   String className = bean.getClass().getName();
   String rootElementName = null;
   if (bean.getClass().isAnnotationPresent(ObjectXmlAlias.class)) {
     AnnotatedElement annotatedElement = bean.getClass();
     ObjectXmlAlias aliasAnnotation = annotatedElement.getAnnotation(ObjectXmlAlias.class);
     rootElementName = aliasAnnotation.value();
   }
   // Use the package name as the namespace URI
   Package pkg = bean.getClass().getPackage();
   nsURI = pkg.getName();
   // Remove a trailing semi-colon (;) if present (i.e. if the bean is an array)
   className = StringUtils.deleteTrailingChar(className, ';');
   StringBuffer sb = new StringBuffer(className);
   String objectName = sb.delete(0, sb.lastIndexOf(".") + 1).toString();
   domDocument = createDomDocument(objectName);
   document = domDocument.getDocument();
   Element root = document.getDocumentElement();
   // Parse the bean elements
   getBeanElements(root, rootElementName, className, bean);
   StringBuffer xml = new StringBuffer();
   if (prettyPrint)
     xml.append(domDocument.serialize(lineSeperator, indentChars, includeXmlProlog));
   else xml.append(domDocument.serialize(includeXmlProlog));
   if (!includeTypeInfo) {
     int index = xml.indexOf(root.getNodeName());
     xml.delete(index - 1, index + root.getNodeName().length() + 2);
     xml.delete(xml.length() - root.getNodeName().length() - 4, xml.length());
   }
   return xml.toString();
 }
Ejemplo n.º 2
0
  /**
   * Returns the correct implementation instance for the interface <code>type</code>. For convention
   * the implentation is named <code>InterfaceName + Impl</code>.
   *
   * @param type The interface type
   * @return The correct ModelProxy subclass (if exists), a ModelProxy instance otherwise
   * @throws MalformedModelException is the interface or the implementation are not well formed
   *     (they don't respect the conventions).
   * @throws ModelRuntimeException is any error occurs during the instantiation
   */
  protected ModelProxy createInstance(Class type) {
    Class backClass;
    if (implementations.containsKey(type)) backClass = implementations.get(type);
    else {
      /* type never seen */

      try {
        Package pkg = type.getPackage();
        String pkgN = pkg == null ? "" : pkg.getName();

        backClass = Class.forName(pkgN + tableName(type) + CommonStatic.getImplementationSuffix());

      } catch (Exception e) {
        backClass = ModelProxy.class;
      }

      Validator.validateModel(type, backClass);

      initFieldsTypes(type);
      implementations.put(type, backClass);
    }

    ModelProxy impl = null;
    try {
      impl = (ModelProxy) backClass.newInstance();
    } catch (Exception e) {
      throw new ModelRuntimeException(e.getMessage());
    }

    return impl;
  }
  /** {@inheritDoc} */
  @Override
  public void start() throws GridException {
    if (ctx.isEnterprise()) {
      Package pkg = getClass().getPackage();

      if (pkg == null)
        throw new GridException(
            "Internal error (package object was not found) for: " + getClass().getName());

      if (ctx.isEnterprise()) {
        try {
          Class<?> cls = Class.forName(pkg.getName() + ".GridEnterpriseSecureSessionHandler");

          sesHnd =
              (GridSecureSessionHandler)
                  cls.getConstructor(GridSecureSessionSpi[].class)
                      .newInstance(new Object[] {getProxies()});
        } catch (ClassNotFoundException e) {
          throw new GridException(
              "Failed to create enterprise secure session handler (implementing class "
                  + "was not found)",
              e);
        } catch (InvocationTargetException e) {
          throw new GridException(
              "Failed to create enterprise secure session handler (target constructor "
                  + "has thrown an exception",
              e.getCause());
        } catch (InstantiationException e) {
          throw new GridException(
              "Failed to create enterprise secure session handler (object cannot be "
                  + "instantiated)",
              e);
        } catch (NoSuchMethodException e) {
          throw new GridException(
              "Failed to create enterprise secure session handler (target constructor "
                  + "could not be found)",
              e);
        } catch (IllegalAccessException e) {
          throw new GridException(
              "Failed to create enterprise secure session handler (object access is not"
                  + " allowed)",
              e);
        }
      }
    } else sesHnd = new GridCommunitySecureSessionHandler();

    startSpi();

    if (log.isDebugEnabled()) log.debug(startInfo());
  }
  protected void loadClassDependencies(Class aClass) throws ClassNotFoundException {
    String name = aClass.getName();
    if (myVisited.add(aClass)) {
      try {
        for (Method method : aClass.getDeclaredMethods()) {
          loadTypeDependencies(method.getGenericReturnType());
          for (Type type : method.getGenericExceptionTypes()) {
            loadTypeDependencies(type);
          }
          for (Type type : method.getGenericParameterTypes()) {
            loadTypeDependencies(type);
          }
        }
        for (Constructor method : aClass.getDeclaredConstructors()) {
          for (Type type : method.getGenericExceptionTypes()) {
            loadTypeDependencies(type);
          }
          for (Type type : method.getGenericParameterTypes()) {
            loadTypeDependencies(type);
          }
        }

        for (Field field : aClass.getDeclaredFields()) {
          loadTypeDependencies(field.getGenericType());
        }

        Type superclass = aClass.getGenericSuperclass();
        if (superclass != null) {
          loadClassDependencies(aClass);
        }

        for (Type intf : aClass.getGenericInterfaces()) {
          loadTypeDependencies(intf);
        }

        aClass.getAnnotations();
        Package aPackage = aClass.getPackage();
        if (aPackage != null) {
          aPackage.getAnnotations();
        }
      } catch (LinkageError e) {
        throw new ClassNotFoundException(name);
      } catch (TypeNotPresentException e) {
        throw new ClassNotFoundException(name);
      }
    }
  }
 public String getPublicMethodsForClasses(String classNames[]) {
   StringBuilder result = new StringBuilder();
   String className = null;
   result.append("<classDefinitions>");
   if (classNames != null && classNames.length > 0) {
     for (int i = 0; i < classNames.length; i++) {
       className = classNames[i];
       if (className != null) {
         result.append("<classDefinition>");
         try {
           Class c = Class.forName(className);
           result.append(
               (new StringBuilder("<classSimpleName>"))
                   .append(c.getSimpleName())
                   .append("</classSimpleName>")
                   .toString());
           result.append(
               (new StringBuilder("<classFullName>"))
                   .append(c.getName())
                   .append("</classFullName>")
                   .toString());
           Package pack = c.getPackage();
           String packStr = "";
           if (pack != null) packStr = pack.getName();
           result.append(
               (new StringBuilder("<packageName>"))
                   .append(packStr)
                   .append("</packageName>")
                   .toString());
           Method methods[] = c.getMethods();
           Method method = null;
           result.append("<methods>");
           if (methods != null) {
             for (int j = 0; j < methods.length; j++) {
               method = methods[j];
               if (method != null && !methodsExclude.contains(method.getName())) {
                 result.append("<method>");
                 result.append(
                     (new StringBuilder("<methodSignature>"))
                         .append(method.toString())
                         .append("</methodSignature>")
                         .toString());
                 result.append(
                     (new StringBuilder("<methodName>"))
                         .append(method.getName())
                         .append("</methodName>")
                         .toString());
                 result.append(
                     (new StringBuilder("<returnType>"))
                         .append(method.getReturnType().getName())
                         .append("</returnType>")
                         .toString());
                 Class paramClasses[] = method.getParameterTypes();
                 result.append("<params>");
                 if (paramClasses != null) {
                   for (int l = 0; l < paramClasses.length; l++)
                     if (paramClasses[l] != null)
                       result.append(
                           (new StringBuilder("<param>"))
                               .append(paramClasses[l].getName())
                               .append("</param>")
                               .toString());
                 }
                 result.append("</params>");
                 result.append("</method>");
               }
             }
           }
           result.append("</methods>");
         } catch (ClassNotFoundException e) {
           result.append(
               (new StringBuilder("<classFullName>"))
                   .append(className)
                   .append("</classFullName>")
                   .toString());
           result.append(
               (new StringBuilder("<error>Problem retrieving "))
                   .append(className)
                   .append(" information</error>")
                   .toString());
           System.out.println(e.getMessage());
         }
         result.append("</classDefinition>");
       }
     }
   }
   result.append("</classDefinitions>");
   return result.toString();
 }
  public String getPublicPropertiesForClasses(String classNames[]) {
    StringBuilder result = new StringBuilder();
    String className = null;
    ArrayList publicFields = new ArrayList();
    result.append("<classDefinitions>");
    if (classNames != null && classNames.length > 0) {
      for (int i = 0; i < classNames.length; i++) {
        className = classNames[i];
        if (className != null) {
          result.append("<classDefinition>");
          try {
            Class c = Class.forName(className);
            Field fields[] = c.getFields();
            Field field = null;
            if (fields != null) {
              for (int k = 0; k < fields.length; k++) {
                field = fields[k];
                if (field != null)
                  publicFields.add(
                      (new StringBuilder(String.valueOf(field.getName())))
                          .append(",")
                          .append(field.getType().getName())
                          .toString());
              }
            }
            try {
              BeanInfo b = Introspector.getBeanInfo(c);
              result.append(
                  (new StringBuilder("<classSimpleName>"))
                      .append(c.getSimpleName())
                      .append("</classSimpleName>")
                      .toString());
              result.append(
                  (new StringBuilder("<classFullName>"))
                      .append(c.getName())
                      .append("</classFullName>")
                      .toString());
              Package pack = c.getPackage();
              String packStr = "";
              if (pack != null) packStr = pack.getName();
              result.append(
                  (new StringBuilder("<packageName>"))
                      .append(packStr)
                      .append("</packageName>")
                      .toString());
              PropertyDescriptor pds[] = b.getPropertyDescriptors();
              if (pds != null) {
                for (int propCount = 0; propCount < pds.length; propCount++) {
                  PropertyDescriptor pd = pds[propCount];
                  String propertyName = pd.getName();
                  Method readMethod = pd.getReadMethod();
                  Method writeMethod = pd.getWriteMethod();
                  if (readMethod != null
                      && isPublicAccessor(readMethod.getModifiers())
                      && writeMethod != null
                      && isPublicAccessor(writeMethod.getModifiers()))
                    publicFields.add(
                        (new StringBuilder(String.valueOf(propertyName)))
                            .append(",")
                            .append(pd.getPropertyType().getName())
                            .toString());
                }
              }
            } catch (Exception e) {
              e.printStackTrace();
            }
            if (publicFields != null && publicFields.size() > 0) {
              String temp = null;
              result.append("<publicFields>");
              for (int counter = 0; counter < publicFields.size(); counter++) {
                temp = (String) publicFields.get(counter);
                if (temp != null) {
                  String pubTemp[] = temp.split(",");
                  if (pubTemp.length == 2) {
                    result.append("<publicField>");
                    result.append(
                        (new StringBuilder("<publicFieldName>"))
                            .append(pubTemp[0])
                            .append("</publicFieldName>")
                            .toString());
                    result.append(
                        (new StringBuilder("<publicFieldType>"))
                            .append(pubTemp[1])
                            .append("</publicFieldType>")
                            .toString());
                    result.append("</publicField>");
                  }
                }
              }

              result.append("</publicFields>");
            }
          } catch (ClassNotFoundException e) {
            result.append(
                (new StringBuilder("<classFullName>"))
                    .append(className)
                    .append("</classFullName>")
                    .toString());
            result.append(
                (new StringBuilder("<error>Problem retrieving "))
                    .append(className)
                    .append(" information</error>")
                    .toString());
            System.out.println(e.getMessage());
          }
          result.append("</classDefinition>");
        }
      }
    }
    result.append("</classDefinitions>");
    return result.toString();
  }