Package org.rhq.core.domain.content

Examples of org.rhq.core.domain.content.PackageVersion


        long timestamp = System.currentTimeMillis();

        // Load the InstalledPackage to get its package version for the relationship
        InstalledPackage ip = entityManager.find(InstalledPackage.class, installedPackageId);
        PackageVersion packageVersion = ip.getPackageVersion();

        // Create the history entity
        InstalledPackageHistory history = new InstalledPackageHistory();
        history.setContentServiceRequest(persistedRequest);
        history.setPackageVersion(packageVersion);
View Full Code Here


            return;
        }
        Resource resource = persistedRequest.getResource();

        InstalledPackageHistory initialRequestHistory = persistedRequest.getInstalledPackageHistory().iterator().next();
        PackageVersion packageVersion = initialRequestHistory.getPackageVersion();

        if (response.getStatus() == ContentRequestStatus.SUCCESS) {
            // Read the stream from the agent and store in the package version
            try {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Saving content for response: " + response);
                }

                PackageBits packageBits = initializePackageBits(null);

                // Could use the following, but only on jdk6 as builds
                // @since 1.6
                // void setBinaryStream(int parameterIndex, java.io.InputStream x) throws SQLException;

                Long length = packageVersion.getFileSize();
                if (length == null) {
                    File tmpFile = File.createTempFile("rhq", ".stream");
                    FileOutputStream fos = new FileOutputStream(tmpFile);
                    length = StreamUtil.copy(bitStream, fos, true);

                    bitStream = new FileInputStream(tmpFile);
                }
                Connection conn = null;
                PreparedStatement ps = null;

                try {
                    PackageBits bits = entityManager.find(PackageBits.class, packageBits.getId());
                    String pkgName = "(set packageName)";
                    if ((packageVersion != null) && (packageVersion.getGeneralPackage() != null)) {
                        //update it to whatever package name is if we can get to it.
                        pkgName = packageVersion.getGeneralPackage().getName();
                    }
                    bits = loadPackageBits(bitStream, packageVersion.getId(), pkgName, packageVersion.getVersion(),
                        bits, null);

                    entityManager.merge(bits);
                } finally {
View Full Code Here

            if (persistedPackageList.size() != 1) {
                LOG.error("Multiple packages found. Found: " + persistedPackageList.size() + " for key: " + key);
            }

            // Convert to transfer object to be sent to the agent
            PackageVersion packageVersion = (PackageVersion) persistedPackageList.get(0);
            ResourcePackageDetails details = ContentManagerHelper.packageVersionToDetails(packageVersion);
            dependencies.add(details);

            // Create an installed package history and attach to the request
            InstalledPackageHistory dependencyPackage = new InstalledPackageHistory();
View Full Code Here

        packageVersionQuery.setParameter("version", version);

        // Result of the query should be either 0 or 1
        List existingVersionList = packageVersionQuery.getResultList();
        if (existingVersionList.size() > 0) {
            PackageVersion existingPackageVersion = (PackageVersion) existingVersionList.get(0);
            if (displayVersion != null && !displayVersion.trim().isEmpty()) {
                existingPackageVersion.setDisplayVersion(displayVersion);
                existingPackageVersion = persistOrMergePackageVersionSafely(existingPackageVersion);
            }

            return existingPackageVersion;
        }

        Architecture architecture = entityManager.find(Architecture.class, architectureId);
        PackageType packageType = entityManager.find(PackageType.class, packageTypeId);

        //check the validity of the provided data
        try {
            PackageTypeBehavior behavior = ContentManagerHelper.getPackageTypeBehavior(packageTypeId);
            ValidatablePackageDetailsKey key = new ValidatablePackageDetailsKey(packageName, version,
                packageType.getName(), architecture.getName());
            behavior.validateDetails(key, subject);

            packageName = key.getName();
            version = key.getVersion();
            if (!architecture.getName().equals(key.getArchitectureName())) {
                Query q = entityManager.createNamedQuery(Architecture.QUERY_FIND_BY_NAME);
                q.setParameter("name", key.getArchitectureName());
                architecture = (Architecture) q.getSingleResult();
            }
        } catch (PackageDetailsValidationException e) {
            throw e;
        } catch (Exception e) {
            LOG.error("Failed to get the package type plugin container. This is a bug.", e);
            throw new IllegalStateException("Failed to get the package type plugin container.", e);
        }

        // If the package doesn't exist, create that here
        Query packageQuery = entityManager.createNamedQuery(Package.QUERY_FIND_BY_NAME_PKG_TYPE_ID);
        packageQuery.setParameter("name", packageName);
        packageQuery.setParameter("packageTypeId", packageTypeId);

        Package existingPackage;

        List existingPackageList = packageQuery.getResultList();

        if (existingPackageList.size() == 0) {
            existingPackage = new Package(packageName, packageType);
            existingPackage = persistOrMergePackageSafely(existingPackage);
        } else {
            existingPackage = (Package) existingPackageList.get(0);
        }

        // Create a package version and add it to the package
        PackageVersion newPackageVersion = new PackageVersion(existingPackage, version, architecture);
        newPackageVersion.setDisplayName(existingPackage.getName());

        newPackageVersion = persistOrMergePackageVersionSafely(newPackageVersion);

        Map<String, String> contentDetails = new HashMap<String, String>();
        PackageBits bits = loadPackageBits(packageBitStream, newPackageVersion.getId(), packageName, version, null,
            contentDetails);

        newPackageVersion.setPackageBits(bits);
        newPackageVersion.setFileSize(Long.valueOf(contentDetails.get(UPLOAD_FILE_SIZE)).longValue());
        newPackageVersion.setSHA256(contentDetails.get(UPLOAD_SHA256));
        newPackageVersion.setDisplayVersion(displayVersion);

        existingPackage.addVersion(newPackageVersion);

        return newPackageVersion;
    }
View Full Code Here

    }

    @Override
    @SuppressWarnings("unchecked")
    public PackageVersion persistOrMergePackageVersionSafely(PackageVersion pv) {
        PackageVersion persisted = null;
        RuntimeException error = null;

        try {
            if (pv.getId() == 0) {
                persisted = contentManager.persistPackageVersion(pv);
            }
        } catch (RuntimeException re) {
            error = re;
        }

        // If we didn't persist, the PV already exists, so we should be able to find it.
        if (persisted == null) {
            Query q = entityManager.createNamedQuery(PackageVersion.QUERY_FIND_BY_PACKAGE_DETAILS_KEY);
            q.setParameter("packageName", pv.getGeneralPackage().getName());
            q.setParameter("packageTypeName", pv.getGeneralPackage().getPackageType().getName());
            q.setParameter("architectureName", pv.getArchitecture().getName());
            q.setParameter("version", pv.getVersion());

            ResourceType rt = pv.getGeneralPackage().getPackageType().getResourceType();
            q.setParameter("resourceType", rt);

            List<PackageVersion> found = q.getResultList();
            if (error != null && found.size() == 0) {
                throw error;
            }
            if (found.size() != 1) {
                throw new RuntimeException("Expecting 1 package version matching [" + pv + "] but got: " + found);
            }

            pv.setId(found.get(0).getId());
            persisted = entityManager.merge(pv);

            if (error != null) {
                LOG.warn("There was probably a very big and ugly EJB/hibernate error just above this log message - "
                    + "you can normally ignore that. We detected that a package version was already created when we"
                    + " tried to do it also - we will ignore this and just use the new package version that was "
                    + "created in the other thread", new Throwable("Stack Trace:"));
            }
        } else {
            // the persisted object is unattached right now,
            // we want it attached so the caller always has an attached entity returned to it
            persisted = entityManager.find(PackageVersion.class, persisted.getId());
            persisted.getGeneralPackage().getId();
            persisted.getArchitecture().getId();
            if (persisted.getExtraProperties() != null) {
                persisted.getExtraProperties().getId();
            }
        }

        return persisted;
    }
View Full Code Here

        PageList<PackageAndLatestVersionComposite> ret = new PageList<PackageAndLatestVersionComposite>(
            packages.getTotalSize(), packages.getPageControl());

        for (Package p : packages) {
            PackageVersion latest = repoManager.getLatestPackageVersion(subject, p.getId(), criteria.getFilterRepoId());
            ret.add(new PackageAndLatestVersionComposite(p, latest));
        }

        return ret;
    }
View Full Code Here

    @SuppressWarnings("unchecked")
    public PackageVersion getUploadedPackageVersion(Subject subject, String packageName, int packageTypeId,
        String version, int architectureId, InputStream packageBitStream, Map<String, String> packageUploadDetails,
        Integer repoId) {

        PackageVersion packageVersion = null;

        //default version to 1.0 if is null, not provided for any reason.
        if ((version == null) || (version.trim().length() == 0)) {
            version = "1.0";
        }

        Architecture architecture = entityManager.find(Architecture.class, architectureId);
        PackageType packageType = entityManager.find(PackageType.class, packageTypeId);

        // See if package version already exists for the resource package
        Query packageVersionQuery = null;

        if (packageType.getResourceType() != null) {
            packageVersionQuery = entityManager
                .createNamedQuery(PackageVersion.QUERY_FIND_BY_PACKAGE_DETAILS_KEY_WITH_NON_NULL_RESOURCE_TYPE);
            packageVersionQuery.setParameter("resourceTypeId", packageType.getResourceType().getId());

        } else {
            packageVersionQuery = entityManager.createNamedQuery(PackageVersion.QUERY_FIND_BY_PACKAGE_DETAILS_KEY);
            packageVersionQuery.setParameter("resourceType", null);
        }

        packageVersionQuery.setFlushMode(FlushModeType.COMMIT);
        packageVersionQuery.setParameter("packageName", packageName);

        packageVersionQuery.setParameter("packageTypeName", packageType.getName());

        packageVersionQuery.setParameter("architectureName", architecture.getName());
        packageVersionQuery.setParameter("version", version);

        // Result of the query should be either 0 or 1
        List<PackageVersion> existingPackageVersionList = packageVersionQuery.getResultList();

        if (existingPackageVersionList.size() > 0) {
            packageVersion = existingPackageVersionList.get(0);
        }

        try {
            PackageTypeBehavior behavior = ContentManagerHelper.getPackageTypeBehavior(packageTypeId);

            if (behavior != null) {
                String packageTypeName = packageType.getName();
                String archName = architecture.getName();
                ValidatablePackageDetailsKey key = new ValidatablePackageDetailsKey(packageName, version,
                    packageTypeName, archName);
                behavior.validateDetails(key, subject);

                //update the details from the validation results
                packageName = key.getName();
                version = key.getVersion();

                if (!architecture.getName().equals(key.getArchitectureName())) {
                    Query q = entityManager.createNamedQuery(Architecture.QUERY_FIND_BY_NAME);
                    q.setParameter("name", key.getArchitectureName());
                    architecture = (Architecture) q.getSingleResult();
                }
            }
        } catch (PackageDetailsValidationException e) {
            throw e;
        } catch (Exception e) {
            LOG.error("Failed to get the package type plugin container. This is a bug.", e);
            throw new IllegalStateException("Failed to get the package type plugin container.", e);
        }

        Package existingPackage = null;

        Query packageQuery = entityManager.createNamedQuery(Package.QUERY_FIND_BY_NAME_PKG_TYPE_ID);
        packageQuery.setParameter("name", packageName);
        packageQuery.setParameter("packageTypeId", packageTypeId);
        List<Package> existingPackageList = packageQuery.getResultList();

        if (existingPackageList.size() == 0) {
            // If the package doesn't exist, create that here
            existingPackage = new Package(packageName, packageType);
            existingPackage = persistOrMergePackageSafely(existingPackage);
        } else {
            existingPackage = existingPackageList.get(0);
        }

        //initialize package version if not already
        if (packageVersion == null) {
            packageVersion = new PackageVersion(existingPackage, version, architecture);
            packageVersion.setDisplayName(existingPackage.getName());
            entityManager.persist(packageVersion);
        }

        //get the data
        Map<String, String> contentDetails = new HashMap<String, String>();
        PackageBits bits = loadPackageBits(packageBitStream, packageVersion.getId(), packageName, version, null,
            contentDetails);

        packageVersion.setPackageBits(bits);

        packageVersion.setFileSize(Long.valueOf(contentDetails.get(UPLOAD_FILE_SIZE)).longValue());
        packageVersion.setSHA256(contentDetails.get(UPLOAD_SHA256));

        //populate extra details, persist
        if (packageUploadDetails != null) {
            packageVersion.setFileCreatedDate(Long.valueOf(packageUploadDetails
                .get(ContentManagerLocal.UPLOAD_FILE_INSTALL_DATE)));
            packageVersion.setFileName(packageUploadDetails.get(ContentManagerLocal.UPLOAD_FILE_NAME));
            packageVersion.setMD5(packageUploadDetails.get(ContentManagerLocal.UPLOAD_MD5));
            packageVersion.setDisplayVersion(packageUploadDetails.get(ContentManagerLocal.UPLOAD_DISPLAY_VERSION));
        }

        entityManager.merge(packageVersion);

        if (repoId != null) {
            int[] packageVersionIds = new int[] { packageVersion.getId() };
            repoManager.addPackageVersionsToRepo(subject, repoId, packageVersionIds);
        }

        entityManager.flush();
View Full Code Here

        // use existing or instantiate PackageBits instance.
        PackageBits bits = (null == existingBits) ? initializePackageBits(null) : existingBits;

        //locate related packageVersion
        PackageVersion pv = entityManager.find(PackageVersion.class, packageVersionId);

        //associate the two if located.
        if (null != pv) {
            pv.setPackageBits(bits);
            entityManager.flush();
        }

        //write data from stream into db using Hibernate Blob mechanism
        updateBlobStream(packageBitStream, bits, contentDetails);
View Full Code Here

        bits.setId(blob.getId());
        bits.setBlob(blob);
        entityManager.flush();

        //locate related packageVersion
        PackageVersion pv = entityManager.find(PackageVersion.class, packageVersionId);

        //associate the two if packageVersion exists.
        if (null != pv) {
            pv.setPackageBits(bits);
            entityManager.flush();
        }

        // update contentDetails in needed
        if (null != contentDetails) {
View Full Code Here

            CriteriaQuery<BundleFile, BundleFileCriteria> bundleFiles = new CriteriaQuery<BundleFile, BundleFileCriteria>(
                bfc, queryExecutor);

            List<PackageVersion> packageVersions = new ArrayList<PackageVersion>();
            PackageVersion packageVersion = null;
            for (BundleFile bundleFile : bundleFiles) {
                pvc.addFilterId(bundleFile.getPackageVersion().getId());
                packageVersion = cm.findPackageVersionsByCriteria(subject, pvc).get(0);
                HibernateDetachUtility.nullOutUninitializedFields(packageVersion, SerializationType.SERIALIZATION);
                packageVersions.add(packageVersion);
View Full Code Here

TOP

Related Classes of org.rhq.core.domain.content.PackageVersion

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.