Search in sources :

Example 16 with DeploymentException

use of org.glassfish.deployment.common.DeploymentException in project Payara by payara.

the class PersistenceDescriptor method initializeFieldInfo.

// Initialize persFieldInfo, pkeyFieldInfo, persNoPkeyFieldInfo,
// pkeyClassPkeyFields
private void initializeFieldInfo() {
    if (!pkeyStuffInitialized)
        initPkeyInfo();
    int cmpFieldCount = cmpFields.size();
    if (cmpFieldCount == 0) {
        throw new DeploymentException("No cmp field defined for CMP EJB " + parentDesc.getName());
    }
    int fkeyCount = 0;
    if (fkeyFields != null)
        fkeyCount = fkeyFields.length;
    persFieldInfo = new PersistentFieldInfo[cmpFieldCount + fkeyCount];
    // Add CMP fields
    int fcount = 0;
    Iterator itr = cmpFields.iterator();
    while (itr.hasNext()) {
        persFieldInfo[fcount] = new PersistentFieldInfo();
        persFieldInfo[fcount].name = ((FieldDescriptor) itr.next()).getName();
        fcount++;
    }
    // Add foreign key fields
    if (fkeyFields != null) {
        for (int i = 0; i < fkeyFields.length; i++) {
            persFieldInfo[fcount] = fkeyFields[i];
            fcount++;
        }
    }
    // sort persFieldInfo in alphabetical order
    for (int i = persFieldInfo.length - 1; i > 0; i--) {
        for (int j = 0; j < i; j++) {
            if (persFieldInfo[j].name.compareTo(persFieldInfo[j + 1].name) > 0) {
                PersistentFieldInfo tmp = persFieldInfo[j];
                persFieldInfo[j] = persFieldInfo[j + 1];
                persFieldInfo[j + 1] = tmp;
            }
        }
    }
    // Initialize pkeyFieldInfo[] and persNoPkeyFieldInfo[]
    // They contain the same PersistentFieldInfo objects as persFieldInfo.
    pkeyFieldInfo = new PersistentFieldInfo[pkeyFields.size()];
    if (pkeyFieldSpecified) {
        // check if PK class has public non-persistent fields
        StringBuffer nonPersFieldsInPK = new StringBuffer();
        for (Iterator it = pkeyFields.iterator(); it.hasNext(); ) {
            FieldDescriptor fd = (FieldDescriptor) it.next();
            boolean isPersistent = false;
            for (int i = 0; i < persFieldInfo.length; i++) {
                if (fd.getName().equals(persFieldInfo[i].name)) {
                    isPersistent = true;
                    break;
                }
            }
            if (!isPersistent) {
                // if not the first non-persistent field
                if (nonPersFieldsInPK.length() != 0) {
                    nonPersFieldsInPK.append(", ");
                }
                nonPersFieldsInPK.append(fd.getName());
            }
        }
        if (nonPersFieldsInPK.length() != 0) {
            throw new DeploymentException(localStrings.getLocalString("enterprise.deployment.pkhasnopersistentfields", "CMP bean [{0}], primary key class [{1}] has " + "public non-persistent field(s) [{2}].", new Object[] { getParentDescriptor().getName(), primaryKeyClassName, nonPersFieldsInPK.toString() }));
        }
        persNoPkeyFieldInfo = new PersistentFieldInfo[persFieldInfo.length - pkeyFieldInfo.length];
        int pkeyCount = 0;
        int noPkeyCount = 0;
        for (int i = 0; i < persFieldInfo.length; i++) {
            boolean isPkey = false;
            for (Iterator it = pkeyFields.iterator(); it.hasNext(); ) {
                FieldDescriptor fd = (FieldDescriptor) it.next();
                if (fd.getName().equals(persFieldInfo[i].name)) {
                    isPkey = true;
                    break;
                }
            }
            if (isPkey)
                pkeyFieldInfo[pkeyCount++] = persFieldInfo[i];
            else
                persNoPkeyFieldInfo[noPkeyCount++] = persFieldInfo[i];
        }
    }
    if (pkeyIsOneField && pkeyFieldSpecified) {
        // Initialize pkey field type. This is needed for
        // beans with no pkey field (i.e. PM-generated pkey)
        // because they wont have get/set for the pkey in the bean class
        // so the getCMPFieldType() below will bomb if type is not set.
        // Note: pkeyFieldInfo and persFieldInfo arrays share the same
        // PersistentFieldInfo objects.
        pkeyFieldInfo[0].type = getPrimaryKeyClass();
    }
    // Initialize Java types in persFieldInfo
    for (int i = 0; i < persFieldInfo.length; i++) {
        // (fkey types will already be filled)
        if (persFieldInfo[i].type == null)
            persFieldInfo[i].type = getCMPFieldType(persFieldInfo[i].name);
    }
    // values in PersistentFieldInfo can only be initialized at runtime.
    try {
        if (persistentClass != null && !Modifier.isAbstract(persistentClass.getModifiers())) {
            for (int i = 0; i < persFieldInfo.length; i++) {
                persFieldInfo[i].field = getField(getStateClass(), persFieldInfo[i].name);
            }
        }
        // Initialize pkeyClassPkeyFields
        if (!pkeyIsOneField && primaryKeyClass != null && !Modifier.isAbstract(primaryKeyClass.getModifiers())) {
            pkeyClassPkeyFields = new Field[pkeyFieldInfo.length];
            for (int i = 0; i < pkeyFieldInfo.length; i++) {
                pkeyClassPkeyFields[i] = primaryKeyClass.getField(pkeyFieldInfo[i].name);
            }
        }
    } catch (NoSuchFieldException ex) {
        if (DOLUtils.getDefaultLogger().isLoggable(Level.FINE)) {
            DOLUtils.getDefaultLogger().log(Level.FINE, ex.toString(), ex);
        }
        throw new DeploymentException(ex);
    }
    fieldInfoInitialized = true;
}
Also used : Iterator(java.util.Iterator) DeploymentException(org.glassfish.deployment.common.DeploymentException)

Example 17 with DeploymentException

use of org.glassfish.deployment.common.DeploymentException in project Payara by payara.

the class JPADeployer method iterateInitializedPUsAtApplicationPrepare.

/**
 * Does java2db on DAS and saves emfs created during prepare to ApplicationInfo maintained by DOL.
 * ApplicationInfo is not available during prepare() so we can not directly use it there.
 * @param context
 */
private void iterateInitializedPUsAtApplicationPrepare(final DeploymentContext context) {
    final DeployCommandParameters deployCommandParameters = context.getCommandParameters(DeployCommandParameters.class);
    String appName = deployCommandParameters.name;
    final ApplicationInfo appInfo = applicationRegistry.get(appName);
    // iterate through all the PersistenceUnitDescriptor for this bundle.
    PersistenceUnitDescriptorIterator pudIterator = new PersistenceUnitDescriptorIterator() {

        @Override
        void visitPUD(PersistenceUnitDescriptor pud, DeploymentContext context) {
            // PersistenceUnitsDescriptor corresponds to  persistence.xml. A bundle can only have one persitence.xml except
            // when the bundle is an application which can have multiple persitence.xml under jars in root of ear and lib.
            PersistenceUnitLoader puLoader = context.getTransientAppMetaData(getUniquePuIdentifier(pud), PersistenceUnitLoader.class);
            if (puLoader != null) {
                // We have initialized PU
                boolean saveEMF = true;
                if (isDas()) {
                    // We do validation and execute Java2DB only on DAS
                    if (deployCommandParameters.origin.isDeploy()) {
                        // APPLICATION_PREPARED will be called for create-application-ref also. We should perform java2db only on first deploy
                        // Create EM to trigger validation on PU
                        EntityManagerFactory emf = puLoader.getEMF();
                        EntityManager em = null;
                        try {
                            // Create EM to trigger any validations that are lazily performed by the provider
                            // EM creation also triggers DDL generation by provider.
                            em = emf.createEntityManager();
                        } catch (PersistenceException e) {
                            // Exception indicates something went wrong while performing validation. Clean up and rethrow to fail deployment
                            emf.close();
                            DeployCommandParameters dcp = context.getCommandParameters(DeployCommandParameters.class);
                            if (dcp.isSkipDSFailure() && ExceptionUtil.isDSFailure(e)) {
                                logger.log(Level.WARNING, "Resource communication failure exception skipped while loading the pu " + pud.getName(), e);
                            } else {
                                // Need to wrap exception in DeploymentException else deployment will not fail !!
                                throw new DeploymentException(e);
                            }
                        } finally {
                            if (em != null) {
                                em.close();
                            }
                        }
                        puLoader.doJava2DB();
                        boolean enabled = deployCommandParameters.enabled;
                        boolean isTargetDas = isTargetDas(deployCommandParameters);
                        if (logger.isLoggable(Level.FINER)) {
                            logger.finer("iterateInitializedPUsAtApplicationPrepare(): enabled: " + enabled + " isTargetDas: " + isTargetDas);
                        }
                        if (!isTargetDas || !enabled) {
                            // we are on DAS but target != das or app is not enabled on das => The EMF was just created for Java2Db. Close it.
                            puLoader.getEMF().close();
                            // Do not save EMF. We have already closed it
                            saveEMF = false;
                        }
                    }
                }
                if (saveEMF) {
                    // Save emf in ApplicationInfo so that it can be retrieved and closed for cleanup
                    // Suppress warning required as there is no way to pass equivalent of List<EMF>.class to the method
                    @SuppressWarnings("unchecked") List<EntityManagerFactory> emfsCreatedForThisApp = appInfo.getTransientAppMetaData(EMF_KEY, List.class);
                    if (emfsCreatedForThisApp == null) {
                        // First EMF for this app, initialize
                        emfsCreatedForThisApp = new ArrayList<EntityManagerFactory>();
                        appInfo.addTransientAppMetaData(EMF_KEY, emfsCreatedForThisApp);
                    }
                    emfsCreatedForThisApp.add(puLoader.getEMF());
                }
            // if (saveEMF)
            }
        // if(puLoader != null)
        }
    };
    pudIterator.iteratePUDs(context);
}
Also used : ApplicationInfo(org.glassfish.internal.data.ApplicationInfo) DeployCommandParameters(org.glassfish.api.deployment.DeployCommandParameters) DeploymentContext(org.glassfish.api.deployment.DeploymentContext) ExtendedDeploymentContext(org.glassfish.internal.deployment.ExtendedDeploymentContext) EntityManager(javax.persistence.EntityManager) EntityManagerFactory(javax.persistence.EntityManagerFactory) PersistenceException(javax.persistence.PersistenceException) DeploymentException(org.glassfish.deployment.common.DeploymentException)

Example 18 with DeploymentException

use of org.glassfish.deployment.common.DeploymentException in project Payara by payara.

the class ResourcesDeployer method preserveResources.

/**
 * preserve the old application's resources so that they can be registered during deploy.
 * @param dc DeploymentContext
 * @param undeployCommandParameters undeploy command parameters
 */
private void preserveResources(DeploymentContext dc, UndeployCommandParameters undeployCommandParameters) {
    try {
        if (undeployCommandParameters.origin == OpsParams.Origin.undeploy) {
            Properties properties = undeployCommandParameters.properties;
            if (properties != null) {
                String preserve = properties.getProperty(DeploymentProperties.PRESERVE_APP_SCOPED_RESOURCES);
                if (preserve != null && Boolean.valueOf(preserve)) {
                    debug("Preserve app scoped resources enabled");
                    final UndeployCommandParameters commandParams = dc.getCommandParameters(UndeployCommandParameters.class);
                    String appName = commandParams.name();
                    Application app = applications.getApplication(appName);
                    preserveResources(app);
                    // store application info (for module information ie., sniffer type)
                    preservedApps.put(appName, app);
                }
            }
        }
    } catch (Exception e) {
        // in the event notification infrastructure
        throw new DeploymentException(e.getMessage(), e);
    }
}
Also used : UndeployCommandParameters(org.glassfish.api.deployment.UndeployCommandParameters) DeploymentException(org.glassfish.deployment.common.DeploymentException) DeploymentProperties(org.glassfish.deployment.common.DeploymentProperties) ResourceException(javax.resource.ResourceException) IOException(java.io.IOException) DeploymentException(org.glassfish.deployment.common.DeploymentException) ResourceConflictException(org.glassfish.resourcebase.resources.api.ResourceConflictException)

Example 19 with DeploymentException

use of org.glassfish.deployment.common.DeploymentException in project Payara by payara.

the class SecurityDeployer method loadPolicy.

/**
 * Translate Web Bundle Policy
 *
 * @param webBD
 * @param remove boolean indicated whether any existing policy statements are removed form context before translation
 * @throws DeploymentException
 */
private void loadPolicy(WebBundleDescriptor webBD, boolean remove) throws DeploymentException {
    try {
        if (webBD != null) {
            if (remove) {
                String cid = SecurityUtil.getContextID(webBD);
                WebSecurityManager wsm = wsmf.getManager(cid, null, true);
                if (wsm != null) {
                    wsm.release();
                }
            }
            wsmf.createManager(webBD, true, serverContext);
        }
    } catch (Exception se) {
        String msg = "Error in generating security policy for " + webBD.getModuleDescriptor().getModuleName();
        throw new DeploymentException(msg, se);
    }
}
Also used : WebSecurityManager(com.sun.enterprise.security.web.integration.WebSecurityManager) DeploymentException(org.glassfish.deployment.common.DeploymentException) IASSecurityException(com.sun.enterprise.security.util.IASSecurityException) DeploymentException(org.glassfish.deployment.common.DeploymentException)

Example 20 with DeploymentException

use of org.glassfish.deployment.common.DeploymentException in project Payara by payara.

the class SecurityDeployer method commitEjbs.

/**
 * commits ejb policy contexts. This should occur in EjbApplication, being done here until issue with
 * ejb-ejb31-singleton-multimoduleApp.ear is resolved
 *
 * @param ejbs
 */
private void commitEjbs(Application app) throws DeploymentException {
    Set<EjbBundleDescriptor> ejbDescriptors = app.getBundleDescriptors(EjbBundleDescriptor.class);
    try {
        for (EjbBundleDescriptor ejbBD : ejbDescriptors) {
            String pcid = SecurityUtil.getContextID(ejbBD);
            ejbProbeProvider.policyCreationStartedEvent(pcid);
            SecurityUtil.generatePolicyFile(pcid);
            ejbProbeProvider.policyCreationEndedEvent(pcid);
            ejbProbeProvider.policyCreationEvent(pcid);
        }
    } catch (Exception se) {
        String msg = "Error in committing security policy for ejbs of " + app.getRegistrationName();
        throw new DeploymentException(msg, se);
    }
}
Also used : EjbBundleDescriptor(com.sun.enterprise.deployment.EjbBundleDescriptor) DeploymentException(org.glassfish.deployment.common.DeploymentException) IASSecurityException(com.sun.enterprise.security.util.IASSecurityException) DeploymentException(org.glassfish.deployment.common.DeploymentException)

Aggregations

DeploymentException (org.glassfish.deployment.common.DeploymentException)30 IASSecurityException (com.sun.enterprise.security.util.IASSecurityException)8 IOException (java.io.IOException)6 Application (com.sun.enterprise.deployment.Application)4 File (java.io.File)4 ResourceException (javax.resource.ResourceException)4 ResourceConflictException (org.glassfish.resourcebase.resources.api.ResourceConflictException)4 Iterator (java.util.Iterator)3 DeployCommandParameters (org.glassfish.api.deployment.DeployCommandParameters)3 Resource (com.sun.enterprise.config.serverbeans.Resource)2 EjbBundleDescriptor (com.sun.enterprise.deployment.EjbBundleDescriptor)2 WebBundleDescriptor (com.sun.enterprise.deployment.WebBundleDescriptor)2 ArrayList (java.util.ArrayList)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 OpsParams (org.glassfish.api.deployment.OpsParams)2 DeploymentProperties (org.glassfish.deployment.common.DeploymentProperties)2 VersioningSyntaxException (org.glassfish.deployment.versioning.VersioningSyntaxException)2 SAXException (org.xml.sax.SAXException)2 SAXParseException (org.xml.sax.SAXParseException)2 ServiceInterfaceGenerator (com.sun.ejb.codegen.ServiceInterfaceGenerator)1