Package org.rhq.core.domain.content.composite

Examples of org.rhq.core.domain.content.composite.LoadedPackageBitsComposite


    public void testPackageBits() throws Throwable {

        executeInTransaction(new TransactionCallback() {

            public void execute() throws Exception {
                LoadedPackageBitsComposite composite;

                try {
                    Resource resource = SessionTestHelper.createNewResource(em, "testPkgBitsResource");
                    PackageType pkgType = new PackageType("testPkgBitsPT", resource.getResourceType());
                    org.rhq.core.domain.content.Package pkg = new Package("testPkgBitsP", pkgType);
                    Architecture arch = new Architecture("testPkgArch");
                    PackageVersion pkgVer = new PackageVersion(pkg, "1", arch);

                    em.persist(pkgType);
                    em.persist(pkg);
                    em.persist(arch);
                    em.persist(pkgVer);
                    em.flush();

                    // test that no bits are available right now
                    composite = contentUIManager.getLoadedPackageBitsComposite(pkgVer.getId());
                    assert composite != null;
                    assert composite.getPackageVersionId() == pkgVer.getId();
                    assert composite.getPackageBitsId() == null;
                    assert !composite.isPackageBitsAvailable();
                    assert !composite.isPackageBitsInDatabase();

                    // pretend we loaded the bits, but we stored them somewhere other then the DB
                    PackageBits packageBits = createPackageBits();
                    pkgVer.setPackageBits(packageBits);
                    pkgVer = em.merge(pkgVer);
                    em.flush();

                    // test that the bits are available, but are not stored in the DB
                    composite = contentUIManager.getLoadedPackageBitsComposite(pkgVer.getId());
                    assert composite != null;
                    assert composite.getPackageVersionId() == pkgVer.getId();
                    assert composite.getPackageBitsId() == packageBits.getId();
                    assert composite.isPackageBitsAvailable();
                    assert !composite.isPackageBitsInDatabase();

                    // let's make sure there really is no data in the DB
                    packageBits = em.find(PackageBits.class, packageBits.getId());
                    assert packageBits != null;
                    assert packageBits.getBlob().getBits() == null;

                    // now lets store some bits in the DB
                    final String DATA = "testPackageBits data";
                    PackageBitsBlob packageBitsBlob = em.find(PackageBitsBlob.class, packageBits.getId());
                    packageBitsBlob.setBits(DATA.getBytes());
                    em.merge(packageBitsBlob);
                    em.flush();

                    // test that the bits are available and stored in the DB
                    composite = contentUIManager.getLoadedPackageBitsComposite(pkgVer.getId());
                    assert composite != null;
                    assert composite.getPackageVersionId() == pkgVer.getId();
                    assert composite.getPackageBitsId() == packageBits.getId();
                    assert composite.isPackageBitsAvailable();
                    assert composite.isPackageBitsInDatabase();

                    // let's make sure the data really is in the DB
                    packageBits = em.find(PackageBits.class, packageBits.getId());
                    assert packageBits != null;
                    assert DATA.equals(new String(packageBits.getBlob().getBits()));

                    ////////////////////////////////////////////////////
                    // create another package version and test with that
                    ////////////////////////////////////////////////////
                    PackageVersion pkgVer2 = new PackageVersion(pkg, "2", arch);
                    em.persist(pkgVer2);
                    em.flush();

                    // first make sure the query still gets the right answer for the first pkgVer
                    composite = contentUIManager.getLoadedPackageBitsComposite(pkgVer.getId());
                    assert composite != null;
                    assert composite.getPackageVersionId() == pkgVer.getId();
                    assert composite.getPackageBitsId() == packageBits.getId();
                    assert composite.isPackageBitsAvailable();
                    assert composite.isPackageBitsInDatabase();

                    // test that no bits are available right now
                    composite = contentUIManager.getLoadedPackageBitsComposite(pkgVer2.getId());
                    assert composite != null;
                    assert composite.getPackageVersionId() == pkgVer2.getId();
                    assert composite.getPackageBitsId() == null;
                    assert !composite.isPackageBitsAvailable();
                    assert !composite.isPackageBitsInDatabase();

                    // pretend we loaded the bits, but we stored them somewhere other then the DB
                    PackageBits packageBits2 = createPackageBits();
                    pkgVer2.setPackageBits(packageBits2);
                    pkgVer2 = em.merge(pkgVer2);
                    em.flush();

                    // make sure the query still gets the right answer for the first pkgVer
                    composite = contentUIManager.getLoadedPackageBitsComposite(pkgVer.getId());
                    assert composite != null;
                    assert composite.getPackageVersionId() == pkgVer.getId();
                    assert composite.getPackageBitsId() == packageBits.getId();
                    assert composite.isPackageBitsAvailable();
                    assert composite.isPackageBitsInDatabase();

                    // test that the bits are available, but are not stored in the DB
                    composite = contentUIManager.getLoadedPackageBitsComposite(pkgVer2.getId());
                    assert composite != null;
                    assert composite.getPackageVersionId() == pkgVer2.getId();
                    assert composite.getPackageBitsId() == packageBits2.getId();
                    assert composite.isPackageBitsAvailable();
                    assert !composite.isPackageBitsInDatabase();

                    // let's make sure there really is no data in the DB
                    packageBits2 = em.find(PackageBits.class, packageBits2.getId());
                    assert packageBits2 != null;
                    assert packageBits2.getBlob().getBits() == null;

                    // now lets store some bits in the DB
                    final String DATA2 = "testPackageBits more data";
                    packageBits2.getBlob().setBits(DATA2.getBytes());
                    em.merge(packageBits2.getBlob());
                    em.flush();

                    // make sure the query still gets the right answer for the first pkgVer
                    composite = contentUIManager.getLoadedPackageBitsComposite(pkgVer.getId());
                    assert composite != null;
                    assert composite.getPackageVersionId() == pkgVer.getId();
                    assert composite.getPackageBitsId() == packageBits.getId();
                    assert composite.isPackageBitsAvailable();
                    assert composite.isPackageBitsInDatabase();

                    // test that the bits are available and stored in the DB
                    composite = contentUIManager.getLoadedPackageBitsComposite(pkgVer2.getId());
                    assert composite != null;
                    assert composite.getPackageVersionId() == pkgVer2.getId();
                    assert composite.getPackageBitsId() == packageBits2.getId();
                    assert composite.isPackageBitsAvailable();
                    assert composite.isPackageBitsInDatabase();

                    // let's make sure the data really is in the DB
                    packageBits2 = em.find(PackageBits.class, packageBits2.getId());
                    assert packageBits2 != null;
                    assert DATA2.equals(new String(packageBits2.getBlob().getBits()));
View Full Code Here


    public void testPackageBitsBlobStream() throws Throwable {

        executeInTransaction(new TransactionCallback() {

            public void execute() throws Exception {
                LoadedPackageBitsComposite composite;
                try {
                    Resource resource = SessionTestHelper.createNewResource(em, "testPkgBitsLargeResource");
                    PackageType pkgType = new PackageType("testPkgBitsLargePT", resource.getResourceType());
                    org.rhq.core.domain.content.Package pkg = new Package("testPkgBitsLargeP", pkgType);
                    Architecture arch = new Architecture("testPkgLargeArch");
                    PackageVersion pkgVer = new PackageVersion(pkg, "1", arch);

                    em.persist(pkgType);
                    em.persist(pkg);
                    em.persist(arch);
                    em.persist(pkgVer);
                    em.flush();

                    // test that no bits are available right now
                    composite = contentUIManager.getLoadedPackageBitsComposite(pkgVer.getId());
                    assert composite != null;
                    assert composite.getPackageVersionId() == pkgVer.getId();
                    assert composite.getPackageBitsId() == null;
                    assert !composite.isPackageBitsAvailable();
                    assert !composite.isPackageBitsInDatabase();

                    // pretend we loaded the bits, but we stored them somewhere other then the DB
                    PackageBits packageBits = createPackageBits();
                    pkgVer.setPackageBits(packageBits);
                    pkgVer = em.merge(pkgVer);
                    em.flush();

                    // test that the bits are available, but are not stored in the DB
                    composite = contentUIManager.getLoadedPackageBitsComposite(pkgVer.getId());
                    assert composite != null;
                    assert composite.getPackageVersionId() == pkgVer.getId();
                    assert composite.getPackageBitsId() == packageBits.getId();
                    assert composite.isPackageBitsAvailable();
                    assert !composite.isPackageBitsInDatabase();

                    // let's make sure there really is no data in the DB
                    packageBits = em.find(PackageBits.class, packageBits.getId());
                    assert packageBits != null;
                    assert packageBits.getBlob().getBits() == null;

                    // now lets store some bits in the DB using PreparedStatements and BLOB mechanism
                    // to simulate large file transfers where streaming is used instead of reading entire
                    // contents into memory every time.

                    // destination once pulled from db
                    File tempDir = getTempDir();
                    if (!tempDir.exists()) {
                        assertTrue("Unable to mkdirs " + tempDir + " for test.", tempDir.mkdirs());
                    }
                    File retrieved = new File(tempDir, "pulled.jar");
                    if (retrieved.exists()) {
                        assertTrue("Unable to delete " + retrieved.getPath() + " for test cleanup.", retrieved.delete());
                    }

                    //any jar should be fine. Use canned jar
                    InputStream originalBinaryStream = this.getClass().getClassLoader()
                        .getResourceAsStream("binary-blob-sample.jar");
                    String originalDigest = new MessageDigestGenerator(MessageDigestGenerator.SHA_256)
                        .calcDigestString(originalBinaryStream);
                    originalBinaryStream.close();
                    originalBinaryStream = this.getClass().getClassLoader()
                        .getResourceAsStream("binary-blob-sample.jar");
                    contentManager.updateBlobStream(originalBinaryStream, packageBits, null);
                    packageBits = em.find(PackageBits.class, packageBits.getId());

                    // test that the bits are available and stored in the DB: Reading the Blob
                    composite = contentUIManager.getLoadedPackageBitsComposite(pkgVer.getId());
                    assert composite != null;
                    assert composite.getPackageVersionId() == pkgVer.getId();
                    assert composite.getPackageBitsId() == packageBits.getId();
                    assert composite.isPackageBitsAvailable();
                    assert composite.isPackageBitsInDatabase();

                    FileOutputStream outputStream = new FileOutputStream(retrieved);
                    contentManager.writeBlobOutToStream(outputStream, packageBits, false);

                    //Check that db content equal to file system content
View Full Code Here

        int packageVersionId = ((Integer) query.getSingleResult()).intValue();

        Query queryA = entityManager.createNamedQuery(PackageBits.QUERY_PACKAGE_BITS_LOADED_STATUS_PACKAGE_VERSION_ID);
        queryA.setParameter("id", packageVersionId);

        LoadedPackageBitsComposite composite = (LoadedPackageBitsComposite) queryA.getSingleResult();

        boolean packageBitsAreAvailable = composite.isPackageBitsAvailable();
        if (packageBitsAreAvailable) {
            // it says the package bits are available, but if its stored on the filesystem, we should
            // make sure no one deleted the file.  If the file is deleted, let's simply download it again.
            if (!composite.isPackageBitsInDatabase()) {
                try {
                    File bitsFile = getPackageBitsLocalFileAndCreateParentDir(composite.getPackageVersionId(),
                        composite.getFileName());
                    if (!bitsFile.exists()) {
                        log.warn("Package version [" + packageDetailsKey + "] has had its bits file [" + bitsFile
                            + "] deleted. Will attempt to download it again.");
                        packageBitsAreAvailable = false;
                    }
View Full Code Here

        //       Or should we not bother to perform this check?  if the caller knows the PV ID, it
        //       probably already got it through its repos

        Query query = entityManager.createNamedQuery(PackageBits.QUERY_PACKAGE_BITS_LOADED_STATUS_PACKAGE_VERSION_ID);
        query.setParameter("id", packageVersionId);
        LoadedPackageBitsComposite composite = (LoadedPackageBitsComposite) query.getSingleResult();

        boolean packageBitsAreAvailable = composite.isPackageBitsAvailable();
        if (packageBitsAreAvailable) {
            // it says the package bits are available, but if its stored on the filesystem, we should
            // make sure no one deleted the file.  If the file is deleted, let's simply download it again.
            if (!composite.isPackageBitsInDatabase()) {
                try {
                    File bitsFile = getPackageBitsLocalFileAndCreateParentDir(composite.getPackageVersionId(),
                        composite.getFileName());
                    if (!bitsFile.exists()) {
                        log.warn("Package version [" + packageDetailsKey + "] has had its bits file [" + bitsFile
                            + "] deleted. Will attempt to download it again.");
                        packageBitsAreAvailable = false;
                    }
                } catch (Exception e) {
                    throw new RuntimeException("Package version [" + packageDetailsKey
                        + "] has had its bits file deleted but cannot download it again.", e);
                }
            }
        }

        PackageVersionContentSource pvcs = null; // will be non-null only if package bits were not originally available

        if (!packageBitsAreAvailable) {
            if (resourceId == -1) {
                throw new IllegalStateException("Package bits must be inserted prior to the agent asking for them "
                    + "during a cotent-based resource creation");
            }
            // if we got here, the package bits have not been downloaded yet.  This eliminates the
            // possibility that the package version were directly uploaded by a user
            // or auto-discovered by a resource and attached to a repo. So, that leaves
            // the only possibility - the package version comes from a content source and therefore has
            // a PackageVersionContentSource mapping.  Let's find that mapping.
            Query q2 = entityManager.createNamedQuery(PackageVersionContentSource.QUERY_FIND_BY_PKG_VER_ID_AND_RES_ID);
            q2.setParameter("resourceId", resourceId);
            q2.setParameter("packageVersionId", packageVersionId);
            List<PackageVersionContentSource> pvcss = q2.getResultList();

            // Note that its possible more than one content source can deliver a PV - if a resource is subscribed
            // to repo(s) that contain multiple content sources that can deliver a PV, we just take
            // the first one we find.

            if (pvcss.size() == 0) {
                throw new RuntimeException("Resource [" + resourceId + "] cannot access package version ["
                    + packageDetailsKey + "] - no content source exists to deliver it");
            }

            pvcs = pvcss.get(0);

            // Make it a true EJB call so we suspend our tx and get a new tx.
            // This way, we start with a fresh tx timeout when downloading and this
            // won't affect the time we are in in this method's tx (I hope that's how it works).
            // This is because this method itself may take a long time to send the data to the output stream
            // and we don't want out tx timing out due to the time it takes downloading.
            PackageBits bits = null;
            bits = contentSourceManager.downloadPackageBits(subjectManager.getOverlord(), pvcs);

            if (bits != null) {
                // rerun the query just to make sure we really downloaded it successfully
                query.setParameter("id", pvcs.getPackageVersionContentSourcePK().getPackageVersion().getId());
                composite = (LoadedPackageBitsComposite) query.getSingleResult();

                if (!composite.isPackageBitsAvailable()) {
                    throw new RuntimeException("Failed to download package bits [" + packageDetailsKey
                        + "] for resource [" + resourceId + "]");
                }
            } else {
                // package bits are not loaded and never will be loaded due to content source's download mode == NEVER
                composite = null;
            }
        }

        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet results = null;
        InputStream bitsStream = null;

        try {
            if (composite == null) {
                // this is DownloadMode.NEVER and we are really in pass-through mode, stream directly from adapter
                ContentServerPluginContainer pc = ContentManagerHelper.getPluginContainer();
                ContentProviderManager adapterMgr = pc.getAdapterManager();
                int contentSourceId = pvcs.getPackageVersionContentSourcePK().getContentSource().getId();
                bitsStream = adapterMgr.loadPackageBits(contentSourceId, pvcs.getLocation());
            } else {
                if (composite.isPackageBitsInDatabase()) {
                    // this is  DownloadMode.DATABASE - put the bits in the database

                    conn = dataSource.getConnection();
                    ps = conn.prepareStatement("SELECT BITS FROM " + PackageBits.TABLE_NAME + " WHERE ID = ?");

                    ps.setInt(1, composite.getPackageBitsId());
                    results = ps.executeQuery();
                    results.next();
                    Blob blob = results.getBlob(1);

                    long bytesRetrieved = 0L;
                    if (endByte < 0L) {
                        if (startByte == 0L) {
                            bytesRetrieved = StreamUtil.copy(blob.getBinaryStream(), outputStream, false);
                        }
                    } else {
                        long length = (endByte - startByte) + 1;
                        //InputStream stream = blob.getBinaryStream(startByte, length);  // JDK 6 api
                        InputStream stream = blob.getBinaryStream();
                        bytesRetrieved = StreamUtil.copy(stream, outputStream, startByte, length);
                    }
                    log.debug("Retrieved and sent [" + bytesRetrieved + "] bytes for [" + packageDetailsKey + "]");
                    ps.close();
                    conn.close();
                    return bytesRetrieved;

                } else {
                    // this is  DownloadMode.FILESYSTEM - put the bits on the filesystem
                    File bitsFile = getPackageBitsLocalFileAndCreateParentDir(composite.getPackageVersionId(),
                        composite.getFileName());
                    if (!bitsFile.exists()) {
                        throw new RuntimeException("Package bits at [" + bitsFile + "] are missing for ["
                            + packageDetailsKey + "]");
                    }
View Full Code Here

    // ContentUIManagerLocal Implementation  --------------------------------------------

    public LoadedPackageBitsComposite getLoadedPackageBitsComposite(int packageVersionId) {
        Query query = entityManager.createNamedQuery(PackageBits.QUERY_PACKAGE_BITS_LOADED_STATUS_PACKAGE_VERSION_ID);
        query.setParameter("id", packageVersionId);
        LoadedPackageBitsComposite composite = (LoadedPackageBitsComposite) query.getSingleResult();
        return composite;
    }
View Full Code Here

TOP

Related Classes of org.rhq.core.domain.content.composite.LoadedPackageBitsComposite

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.