Search in sources :

Example 31 with OpenEJBException

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

the class EjbJarInfoBuilder method initRelationshipRole.

private CmrFieldInfo initRelationshipRole(final EjbRelationshipRole role, final EjbRelationshipRole relatedRole, final Map<String, EnterpriseBeanInfo> infos) throws OpenEJBException {
    final CmrFieldInfo cmrFieldInfo = new CmrFieldInfo();
    // find the entityBeanInfo info for this role
    final String ejbName = role.getRelationshipRoleSource().getEjbName();
    final EnterpriseBeanInfo enterpriseBeanInfo = infos.get(ejbName);
    if (enterpriseBeanInfo == null) {
        throw new OpenEJBException("Relation role source ejb not found " + ejbName);
    }
    if (!(enterpriseBeanInfo instanceof EntityBeanInfo)) {
        throw new OpenEJBException("Relation role source ejb is not an entity bean " + ejbName);
    }
    final EntityBeanInfo entityBeanInfo = (EntityBeanInfo) enterpriseBeanInfo;
    cmrFieldInfo.roleSource = entityBeanInfo;
    // RoleName: this may be null
    cmrFieldInfo.roleName = role.getEjbRelationshipRoleName();
    cmrFieldInfo.synthetic = role.getCmrField() == null;
    // CmrFieldName: is null for uni-directional relationships
    if (role.getCmrField() != null) {
        cmrFieldInfo.fieldName = role.getCmrField().getCmrFieldName();
        // CollectionType: java.util.Collection or java.util.Set
        if (role.getCmrField().getCmrFieldType() != null) {
            cmrFieldInfo.fieldType = role.getCmrField().getCmrFieldType().toString();
        }
        if (cmrFieldInfo.fieldType == null && relatedRole.getMultiplicity() == Multiplicity.MANY) {
            cmrFieldInfo.fieldType = Collection.class.getName();
        }
    } else {
        final String relatedEjbName = relatedRole.getRelationshipRoleSource().getEjbName();
        final EnterpriseBeanInfo relatedEjb = infos.get(relatedEjbName);
        if (relatedEjb == null) {
            throw new OpenEJBException("Relation role source ejb not found " + relatedEjbName);
        }
        if (!(relatedEjb instanceof EntityBeanInfo)) {
            throw new OpenEJBException("Relation role source ejb is not an entity bean " + relatedEjbName);
        }
        final EntityBeanInfo relatedEntity = (EntityBeanInfo) relatedEjb;
        relatedRole.getRelationshipRoleSource();
        cmrFieldInfo.fieldName = relatedEntity.abstractSchemaName + "_" + relatedRole.getCmrField().getCmrFieldName();
        if (relatedRole.getMultiplicity() == Multiplicity.MANY) {
            cmrFieldInfo.fieldType = Collection.class.getName();
        }
    }
    // CascadeDelete
    cmrFieldInfo.cascadeDelete = role.getCascadeDelete();
    // Multiplicity: one or many
    cmrFieldInfo.many = role.getMultiplicity() == Multiplicity.MANY;
    // add the field to the entityBean
    entityBeanInfo.cmrFields.add(cmrFieldInfo);
    return cmrFieldInfo;
}
Also used : EnterpriseBeanInfo(org.apache.openejb.assembler.classic.EnterpriseBeanInfo) OpenEJBException(org.apache.openejb.OpenEJBException) CmrFieldInfo(org.apache.openejb.assembler.classic.CmrFieldInfo) Collection(java.util.Collection) EntityBeanInfo(org.apache.openejb.assembler.classic.EntityBeanInfo)

Example 32 with OpenEJBException

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

the class Deploy method unpack.

private static File unpack(final File jarFile, final File destinationDir) throws OpenEJBException, DeploymentTerminatedException {
    try {
        checkDest(destinationDir, jarFile);
        JarExtractor.extract(jarFile, destinationDir);
        return destinationDir;
    } catch (final IOException e) {
        throw new OpenEJBException("Unable to extract jar. " + e.getMessage(), e);
    }
}
Also used : OpenEJBException(org.apache.openejb.OpenEJBException) IOException(java.io.IOException)

Example 33 with OpenEJBException

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

the class Deploy method main.

public static void main(final String... args) throws SystemExitException {
    final CommandLineParser parser = new PosixParser();
    // create the Options
    final Options options = new Options();
    options.addOption(option("v", "version", "cmd.deploy.opt.version"));
    options.addOption(option("h", "help", "cmd.deploy.opt.help"));
    options.addOption(option("o", "offline", "cmd.deploy.opt.offline"));
    options.addOption(option("s", "server-url", "url", "cmd.deploy.opt.server"));
    options.addOption(option("d", "debug", "cmd.deploy.opt.debug"));
    options.addOption(option("q", "quiet", "cmd.deploy.opt.quiet"));
    options.addOption(option("u", "undeploy", "cmd.deploy.opt.undeploy"));
    options.addOption(option(null, "dir", "cmd.deploy.opt.dir"));
    final CommandLine line;
    try {
        // parse the command line arguments
        line = parser.parse(options, args);
    } catch (final ParseException exp) {
        help(options);
        throw new SystemExitException(-1);
    }
    if (line.hasOption("help")) {
        help(options);
        return;
    } else if (line.hasOption("version")) {
        OpenEjbVersion.get().print(System.out);
        return;
    }
    if (line.getArgList().size() == 0) {
        System.out.println("Must specify an archive to deploy.");
        help(options);
        return;
    }
    // make sure that the modules given on the command line are accessible
    final List<?> modules = line.getArgList();
    for (final Object module : modules) {
        final String path = (String) module;
        final File file = new File(path);
        try {
            checkSource(file);
        } catch (final DeploymentTerminatedException e) {
            System.out.println(e.getMessage());
            // TODO: What is it for?
            throw new SystemExitException(-100);
        }
    }
    final boolean offline = line.hasOption("offline");
    final File apps;
    try {
        final String dir = line.getOptionValue("dir", "apps");
        apps = SystemInstance.get().getBase().getDirectory(dir);
    } catch (final IOException e) {
        throw new SystemExitException(-1);
    }
    if (!apps.exists()) {
        System.out.println("Directory does not exist: " + apps.getAbsolutePath());
    }
    Deployer deployer = null;
    if (!offline) {
        final Properties p = new Properties();
        p.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.RemoteInitialContextFactory");
        String serverUrl = line.getOptionValue("server-url", defaultServerUrl);
        if ("auto".equalsIgnoreCase(serverUrl.trim())) {
            try {
                final File sXml = new File(JavaSecurityManagers.getSystemProperty("openejb.base", "conf/server.xml"));
                if (sXml.exists()) {
                    final QuickServerXmlParser result = QuickServerXmlParser.parse(sXml);
                    serverUrl = "http://" + result.host() + ":" + result.http() + "/tomee/ejb";
                }
            } catch (final Throwable e) {
            // no-op
            }
        }
        p.put(Context.PROVIDER_URL, serverUrl);
        try {
            final InitialContext ctx = new InitialContext(p);
            deployer = (Deployer) ctx.lookup("openejb/DeployerBusinessRemote");
        } catch (final ServiceUnavailableException e) {
            System.out.println(e.getCause().getMessage());
            System.out.println(messages.format("cmd.deploy.serverOffline"));
            throw new SystemExitException(-1);
        } catch (final NamingException e) {
            System.out.println("openejb/DeployerBusinessRemote does not exist in server '" + serverUrl + "', check the server logs to ensure it exists and has not been removed.");
            throw new SystemExitException(-2);
        }
    }
    final boolean undeploy = line.hasOption("undeploy");
    // We increment the exit code once for every failed deploy
    int exitCode = 0;
    for (final Object obj : line.getArgList()) {
        final String path = (String) obj;
        final File file = new File(path);
        File destFile = new File(apps, file.getName());
        try {
            if (shouldUnpack(file)) {
                final File unpacked = unpackedLocation(file, apps);
                if (undeploy) {
                    undeploy(offline, unpacked, path, deployer);
                }
                destFile = unpack(file, unpacked);
            } else {
                if (undeploy) {
                    undeploy(offline, destFile, path, deployer);
                }
                checkDest(destFile, file);
                copyFile(file, destFile);
            }
            if (offline) {
                System.out.println(messages.format("cmd.deploy.offline", path, apps.getAbsolutePath()));
                continue;
            }
            final String location;
            try {
                location = destFile.getCanonicalPath();
            } catch (final IOException e) {
                throw new OpenEJBException(messages.format("cmd.deploy.fileNotFound", path));
            }
            final AppInfo appInfo = deployer.deploy(location);
            System.out.println(messages.format("cmd.deploy.successful", path, appInfo.path));
            if (line.hasOption("quiet")) {
                continue;
            }
            print(appInfo);
        } catch (final UndeployException e) {
            System.out.println(messages.format("cmd.undeploy.failed", path));
            e.printStackTrace(System.out);
            exitCode++;
        } catch (final DeploymentTerminatedException e) {
            System.out.println(e.getMessage());
            exitCode++;
        } catch (final ValidationFailedException e) {
            System.out.println(messages.format("cmd.deploy.validationFailed", path));
            int level = 2;
            if (line.hasOption("debug")) {
                level = 3;
            }
            final AppValidator appValidator = new AppValidator(level, false, true, false);
            appValidator.printResults(e);
            exitCode++;
            if (!delete(destFile)) {
                System.out.println(messages.format("cmd.deploy.cantDelete.deploy", destFile.getAbsolutePath()));
            }
        } catch (final Throwable e) {
            System.out.println(messages.format("cmd.deploy.failed", path));
            e.printStackTrace(System.out);
            exitCode++;
            if (!delete(destFile)) {
                System.out.println(messages.format("cmd.deploy.cantDelete.deploy", destFile.getAbsolutePath()));
            }
        }
    }
    if (exitCode != 0) {
        throw new SystemExitException(exitCode);
    }
}
Also used : Options(org.apache.commons.cli.Options) OpenEJBException(org.apache.openejb.OpenEJBException) PosixParser(org.apache.commons.cli.PosixParser) SystemExitException(org.apache.openejb.cli.SystemExitException) ServiceUnavailableException(javax.naming.ServiceUnavailableException) Properties(java.util.Properties) NamingException(javax.naming.NamingException) CommandLineParser(org.apache.commons.cli.CommandLineParser) Deployer(org.apache.openejb.assembler.Deployer) IOException(java.io.IOException) InitialContext(javax.naming.InitialContext) WebAppInfo(org.apache.openejb.assembler.classic.WebAppInfo) AppInfo(org.apache.openejb.assembler.classic.AppInfo) CommandLine(org.apache.commons.cli.CommandLine) ParseException(org.apache.commons.cli.ParseException) JarFile(java.util.jar.JarFile) File(java.io.File) UndeployException(org.apache.openejb.UndeployException)

Example 34 with OpenEJBException

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

the class PersistenceContextAnnFactory method addAnnotations.

public void addAnnotations(final Class c) throws OpenEJBException {
    if (!useAsm) {
        return;
    }
    if (processed.contains(c.getName())) {
        return;
    }
    try {
        final URL u = c.getResource("/" + c.getName().replace('.', '/') + ".class");
        final ClassReader r = new ClassReader(IO.read(u));
        r.accept(new PersistenceContextReader(contexts), ClassReader.SKIP_DEBUG);
    } catch (final IOException e) {
        throw new OpenEJBException("Unable to read class " + c.getName());
    }
    processed.add(c.getName());
}
Also used : OpenEJBException(org.apache.openejb.OpenEJBException) ClassReader(org.apache.xbean.asm9.ClassReader) IOException(java.io.IOException) URL(java.net.URL)

Example 35 with OpenEJBException

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

the class ModuleProperties method readProperties.

private static void readProperties(final DeploymentModule module) throws OpenEJBException {
    final Object o = module.getAltDDs().get("module.properties");
    if (o instanceof URL) {
        final URL url = (URL) o;
        try {
            final Properties properties = IO.readProperties(url);
            module.getProperties().putAll(properties);
        } catch (final IOException e) {
            throw new OpenEJBException("Cannot read module.properties: " + url, e);
        }
    } else if (o instanceof Properties) {
        module.getProperties().putAll((Properties) o);
    } else if (o != null) {
        throw new OpenEJBException("Unknown module.properties type: " + o.getClass().getName());
    }
}
Also used : OpenEJBException(org.apache.openejb.OpenEJBException) IOException(java.io.IOException) Properties(java.util.Properties) URL(java.net.URL)

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