/* Licensed 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.camunda.bpm.model.xml.impl.util;

import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.net.URL;

import org.camunda.bpm.model.xml.ModelException;


/**
 * @author Tom Baeyens
 */
public abstract class ReflectUtil {

  public static InputStream getResourceAsStream(String name) {
    // Try the current Thread context class loader
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    InputStream resourceStream = classLoader.getResourceAsStream(name);
    if (resourceStream == null) {
      // Finally, try the class loader for this class
      classLoader = ReflectUtil.class.getClassLoader();
      resourceStream = classLoader.getResourceAsStream(name);
    }

    return resourceStream;
  }

  public static URL getResource(String name) {
    // Try the current Thread context class loader
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    URL url = classLoader.getResource(name);
    if (url == null) {
      // Finally, try the class loader for this class
      classLoader = ReflectUtil.class.getClassLoader();
      url = classLoader.getResource(name);
    }

    return url;
  }

  /**
   * Create a new instance of the provided type
   * 
   * @param type the class to create a new instance of 
   * @param parameters the parameters to pass to the constructor
   * @return the created instance
   */
  public static <T> T createInstance(Class<T> type, Object... parameters) {

    // get types for parameters
    Class<?>[] parameterTypes = new Class<?>[parameters.length];
    for (int i = 0; i < parameters.length; i++) {
      Object parameter = parameters[i];
      parameterTypes[i] = parameter.getClass();
    }

    try {
      // create instance
      Constructor<T> constructor = type.getConstructor(parameterTypes);
      return constructor.newInstance(parameters);

    } catch (Exception e) {
      throw new ModelException("Exception while creating an instance of type "+type, e);
    }
  }

}

@@ -1354,32 +1354,177 @@
 (String name) %7B%0A
+    return getResource(name, null);%0A  %7D%0A%0A  public static URL getResource(String name, ClassLoader classLoader) %7B%0A    if(classLoader == null) %7B%0A  
     // Try the c
@@ -1558,35 +1558,25 @@
  loader%0A    
-ClassLoader
+ 
  classLoader
@@ -1622,24 +1622,30 @@
 ssLoader();%0A
+    %7D%0A
     URL url 
@@ -1939,17 +1939,16 @@
 ype%0A   *
- 
 %0A   * @p
@@ -1994,17 +1994,16 @@
 tance of
- 
 %0A   * @p

use bpmn model api classloader