Search in sources :

Example 21 with OpenEJBException

use of org.apache.openejb.OpenEJBException in project tomee by apache.

the class AutoConfig method getType.

private String getType(final JndiReference ref, final ClassLoader classLoader) throws OpenEJBException {
    final String refType = ref.getType();
    if (refType != null) {
        return refType;
    }
    if (classLoader != null) {
        final Set<InjectionTarget> injections = ref.getInjectionTarget();
        for (final InjectionTarget injection : injections) {
            try {
                final Class target = classLoader.loadClass(injection.getInjectionTargetClass().trim());
                final Class type = IntrospectionSupport.getPropertyType(target, injection.getInjectionTargetName().trim());
                return type.getName();
            } catch (final ClassNotFoundException | NoSuchFieldException e) {
            // ignore
            }
        }
    }
    throw new OpenEJBException("Unable to infer type for " + ref.getKey());
}
Also used : OpenEJBException(org.apache.openejb.OpenEJBException) InjectionTarget(org.apache.openejb.jee.InjectionTarget)

Example 22 with OpenEJBException

use of org.apache.openejb.OpenEJBException in project tomee by apache.

the class AppContextConfigDeployer method deploy.

@Override
public AppModule deploy(final AppModule appModule) throws OpenEJBException {
    final Collection<DeploymentModule> deploymentModule = appModule.getDeploymentModule();
    deploymentModule.add(appModule);
    // parse files once since it is application scoped (we don't want duplicates)
    final Set<String> alreadyParsed = new HashSet<>();
    for (final DeploymentModule module : deploymentModule) {
        final Object o = module.getAltDDs().get(CONFIG_NAME);
        if (o instanceof URL) {
            final URL url = (URL) o;
            if (alreadyParsed.add(url.toExternalForm())) {
                configure(appModule, url);
            }
        } else if (o != null) {
            throw new OpenEJBException("Unknown app-ctx.xml type: " + o.getClass().getName());
        }
    }
    alreadyParsed.clear();
    return appModule;
}
Also used : OpenEJBException(org.apache.openejb.OpenEJBException) URL(java.net.URL) HashSet(java.util.HashSet)

Example 23 with OpenEJBException

use of org.apache.openejb.OpenEJBException in project tomee by apache.

the class AnnotationDeployer method addRestClassesToScannedClasses.

private static void addRestClassesToScannedClasses(final WebModule webModule, final Set<Class> classes, final ClassLoader classLoader) throws OpenEJBException {
    for (final String rawClassName : webModule.getRestClasses()) {
        final String className = realClassName(rawClassName);
        if (className != null) {
            final Class<?> clazz;
            try {
                clazz = classLoader.loadClass(className);
                classes.add(clazz);
            } catch (final ClassNotFoundException e) {
                throw new OpenEJBException("Unable to load REST class: " + className, e);
            }
        }
    }
}
Also used : OpenEJBException(org.apache.openejb.OpenEJBException)

Example 24 with OpenEJBException

use of org.apache.openejb.OpenEJBException in project tomee by apache.

the class DeploymentLoader method load.

/**
 * @param jarFile the app file (war, jar, ear)
 * @param config potentially some more config, mainly used when linking to another system like tomcat to enrich the conf we can guess
 * @return the loaded module
 */
public AppModule load(final File jarFile, final ExternalConfiguration config) throws OpenEJBException {
    // verify we have a valid file
    final String jarPath;
    try {
        jarPath = jarFile.getCanonicalPath();
    } catch (final IOException e) {
        throw new OpenEJBException("Invalid application file path " + jarFile, e);
    }
    final URL baseUrl = getFileUrl(jarFile);
    // create a class loader to use for detection of module type
    // do not use this class loader for any other purposes... it is
    // non-temp class loader and usage will mess up JPA
    // = ClassLoaderUtil.createClassLoader(jarPath, new URL[]{baseUrl}, OpenEJB.class.getClassLoader());
    ClassLoader doNotUseClassLoader = null;
    try {
        // determine the module type
        final Class<? extends DeploymentModule> moduleClass;
        try {
            doNotUseClassLoader = ClassLoaderUtil.createClassLoader(jarPath, new URL[] { baseUrl }, getOpenEJBClassLoader());
            moduleClass = discoverModuleType(baseUrl, ClassLoaderUtil.createTempClassLoader(doNotUseClassLoader), true);
        } catch (final Exception e) {
            throw new UnknownModuleTypeException("Unable to determine module type for jar: " + baseUrl.toExternalForm(), e);
        }
        if (ResourcesModule.class.equals(moduleClass)) {
            final AppModule appModule = new AppModule(null, jarPath);
            final ResourcesModule module = new ResourcesModule();
            module.getAltDDs().put(RESOURCES_XML, baseUrl);
            ReadDescriptors.readResourcesXml(module);
            module.initAppModule(appModule);
            // here module is no more useful since everything is in the appmodule
            return appModule;
        }
        // We always load AppModule, as it somewhat likes a wrapper module
        if (AppModule.class.equals(moduleClass)) {
            return createAppModule(jarFile, jarPath);
        }
        if (EjbModule.class.equals(moduleClass)) {
            final URL[] urls = new URL[] { baseUrl };
            SystemInstance.get().fireEvent(new BeforeDeploymentEvent(urls));
            final ClassLoader classLoader = ClassLoaderUtil.createTempClassLoader(jarPath, urls, getOpenEJBClassLoader());
            final AppModule appModule;
            // final Class<? extends DeploymentModule> o = EjbModule.class;
            final EjbModule ejbModule = createEjbModule(baseUrl, jarPath, classLoader);
            // wrap the EJB Module with an Application Module
            appModule = new AppModule(ejbModule);
            addPersistenceUnits(appModule, baseUrl);
            return appModule;
        }
        if (ClientModule.class.equals(moduleClass)) {
            final String jarLocation = URLs.toFilePath(baseUrl);
            final ClientModule clientModule = createClientModule(baseUrl, jarLocation, getOpenEJBClassLoader(), null);
            // Wrap the resource module with an Application Module
            return new AppModule(clientModule);
        }
        if (ConnectorModule.class.equals(moduleClass)) {
            final String jarLocation = URLs.toFilePath(baseUrl);
            final ConnectorModule connectorModule = createConnectorModule(jarLocation, jarLocation, getOpenEJBClassLoader(), null);
            if (connectorModule != null) {
                final List<ConnectorModule> connectorModules = new ArrayList<>();
                // let it be able to deploy the same connector several times
                final String id = connectorModule.getModuleId();
                if (!"true".equalsIgnoreCase(SystemInstance.get().getProperty("openejb.connector." + id + ".skip-default", "false"))) {
                    connectorModules.add(connectorModule);
                }
                final String aliases = SystemInstance.get().getProperty("openejb.connector." + id + ".aliases");
                if (aliases != null) {
                    for (final String alias : aliases.split(",")) {
                        final ConnectorModule aliasModule = createConnectorModule(jarLocation, jarLocation, getOpenEJBClassLoader(), alias);
                        connectorModules.add(aliasModule);
                    }
                }
                // Wrap the resource module with an Application Module
                final AppModule appModule = new AppModule(connectorModules.toArray(new ConnectorModule[connectorModules.size()]));
                return appModule;
            }
        }
        if (WebModule.class.equals(moduleClass)) {
            final File file = URLs.toFile(baseUrl);
            // Standalone Web Module
            final WebModule webModule = createWebModule(file.getAbsolutePath(), baseUrl, getOpenEJBClassLoader(), getContextRoot(), getModuleName(), config);
            // important to use the webapp classloader here otherwise each time we'll check something using loadclass it will fail (=== empty classloader)
            final AppModule appModule = new AppModule(webModule.getClassLoader(), file.getAbsolutePath(), new Application(), true);
            addWebModule(webModule, appModule);
            addWebModuleDescriptors(baseUrl, webModule, appModule);
            appModule.setStandloneWebModule();
            // force it for webapps
            appModule.setDelegateFirst(true);
            return appModule;
        }
        if (PersistenceModule.class.equals(moduleClass)) {
            final String jarLocation = URLs.toFilePath(baseUrl);
            final ClassLoader classLoader = ClassLoaderUtil.createTempClassLoader(jarPath, new URL[] { baseUrl }, getOpenEJBClassLoader());
            // wrap the EJB Module with an Application Module
            final AppModule appModule = new AppModule(classLoader, jarLocation);
            // Persistence Units
            addPersistenceUnits(appModule, baseUrl);
            return appModule;
        }
        throw new UnsupportedModuleTypeException("Unsupported module type: " + moduleClass.getSimpleName());
    } finally {
        // We can safely destroy this class loader in either case, as it was not use by any modules
        if (null != doNotUseClassLoader) {
            ClassLoaderUtil.destroyClassLoader(doNotUseClassLoader);
        }
    }
}
Also used : OpenEJBException(org.apache.openejb.OpenEJBException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) URL(java.net.URL) OpenEJBException(org.apache.openejb.OpenEJBException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) URLClassLoader(java.net.URLClassLoader) EmptyResourcesClassLoader(org.apache.openejb.core.EmptyResourcesClassLoader) JarFile(java.util.jar.JarFile) File(java.io.File) Application(org.apache.openejb.jee.Application) BeforeDeploymentEvent(org.apache.openejb.config.event.BeforeDeploymentEvent)

Example 25 with OpenEJBException

use of org.apache.openejb.OpenEJBException in project tomee by apache.

the class ConfigurationFactory method configureApplication.

public AppInfo configureApplication(final File jarFile) throws OpenEJBException {
    logger.debug("Beginning load: " + jarFile.getAbsolutePath());
    try {
        final AppModule appModule = deploymentLoader.load(jarFile, null);
        final AppInfo appInfo = configureApplication(appModule);
        // this is clean up in Assembler for safety and TomcatWebAppBuilder when used
        if (!appModule.getWebModules().isEmpty()) {
            for (final WebAppInfo info : appInfo.webApps) {
                for (final EjbModule ejbModule : appModule.getEjbModules()) {
                    if (ejbModule.getModuleId().equals(info.moduleId) && ejbModule.getFinder() != null) {
                        appInfo.properties.put(info, ejbModule);
                    }
                }
            }
        }
        // TODO This is temporary -- we need to do this in AppInfoBuilder
        appInfo.paths.add(appInfo.path);
        appInfo.paths.add(jarFile.getAbsolutePath());
        return appInfo;
    } catch (final ValidationFailedException e) {
        // DO not include the stacktrace in the message
        logger.warning("configureApplication.loadFailed", jarFile.getAbsolutePath(), e.getMessage());
        throw e;
    } catch (final OpenEJBException e) {
        // DO NOT REMOVE THE EXCEPTION FROM THIS LOG MESSAGE
        // removing this message causes NO messages to be printed when embedded
        logger.warning("configureApplication.loadFailed", e, jarFile.getAbsolutePath(), e.getMessage());
        throw e;
    }
}
Also used : OpenEJBException(org.apache.openejb.OpenEJBException) WebAppInfo(org.apache.openejb.assembler.classic.WebAppInfo) AppInfo(org.apache.openejb.assembler.classic.AppInfo) WebAppInfo(org.apache.openejb.assembler.classic.WebAppInfo)

Aggregations

OpenEJBException (org.apache.openejb.OpenEJBException)192 IOException (java.io.IOException)54 NamingException (javax.naming.NamingException)35 URL (java.net.URL)31 MalformedURLException (java.net.MalformedURLException)30 BeanContext (org.apache.openejb.BeanContext)30 ApplicationException (org.apache.openejb.ApplicationException)29 ArrayList (java.util.ArrayList)28 File (java.io.File)27 OpenEJBRuntimeException (org.apache.openejb.OpenEJBRuntimeException)24 Method (java.lang.reflect.Method)22 SystemException (org.apache.openejb.SystemException)22 HashMap (java.util.HashMap)18 ThreadContext (org.apache.openejb.core.ThreadContext)18 RemoteException (java.rmi.RemoteException)14 EJBException (javax.ejb.EJBException)14 EjbTransactionUtil.handleApplicationException (org.apache.openejb.core.transaction.EjbTransactionUtil.handleApplicationException)14 HashSet (java.util.HashSet)13 Properties (java.util.Properties)13 EJBAccessException (javax.ejb.EJBAccessException)13