Example usage for java.util Hashtable put

List of usage examples for java.util Hashtable put

Introduction

In this page you can find the example usage for java.util Hashtable put.

Prototype

public synchronized V put(K key, V value) 

Source Link

Document

Maps the specified key to the specified value in this hashtable.

Usage

From source file:com.cws.esolutions.core.utils.NetworkUtils.java

/**
 * Creates an SSH connection to a target host and then executes an SCP
 * request to send or receive a file to or from the target. This is fully
 * key-based, as a result, a keyfile is required for the connection to
 * successfully authenticate./*from  w ww  .j  ava  2s  .c  o  m*/
 * 
 * NOTE: The key file provided MUST be an OpenSSH key. If its not, it must
 * be converted using ssh-keygen:
 * If no passphrase exists on the key: ssh-keygen -i -f /path/to/file
 * If a passphrase exists:
 * Remove the passphrase first: ssh-keygen-g3 -e /path/to/file
 *  - provide passphrase
 * Type 'yes'
 * Type 'no'
 * Type 'yes'
 * Hit enter twice without entering a new passphrase
 * Convert the keyfile: ssh-keygen -i -f /path/to/file > /path/to/new-file
 * Re-encrypt the file: ssh-keygen -p -f /path/to/new-file
 *
 * @param targetHost - The target server to perform the transfer to
 * @param commandList - The list of commands to execute on the remote host.
 * @return <code>StringBuilder</code> containing the response information from the provided commandList
 * @throws UtilityException {@link com.cws.esolutions.core.utils.exception.UtilityException} if an error occurs processing
 */
public static final synchronized StringBuilder executeSshConnection(final String targetHost,
        final String commandList) throws UtilityException {
    final String methodName = NetworkUtils.CNAME
            + "#executeSshConnection(final String targetHost, final String commandList) throws UtilityException";

    if (DEBUG) {
        DEBUGGER.debug(methodName);
        DEBUGGER.debug("Value: {}", targetHost);
        DEBUGGER.debug("Value: {}", commandList);
    }

    Session session = null;
    Channel channel = null;
    StringBuilder sBuilder = null;

    final SSHConfig sshConfig = appBean.getConfigData().getSshConfig();

    if (DEBUG) {
        DEBUGGER.debug("SSHConfig: {}", sshConfig);
    }

    try {
        Hashtable<String, String> sshProperties = new Hashtable<String, String>();
        sshProperties.put("StrictHostKeyChecking", "yes");

        if (DEBUG) {
            DEBUGGER.debug("Hashtable<String, String> sshProperties: {}", sshProperties);
        }

        JSch jsch = new JSch();
        JSch.setConfig(sshProperties);

        if (DEBUG) {
            DEBUGGER.debug("JSch: {}", jsch);
        }

        session = jsch
                .getSession((StringUtils.isNotEmpty(sshConfig.getSshAccount())) ? sshConfig.getSshAccount()
                        : System.getProperty("user.name"), targetHost, 22);

        if (DEBUG) {
            DEBUGGER.debug("Session: {}", session);
        }

        if (StringUtils.isNotEmpty(sshConfig.getSshKey())) {
            if (!(FileUtils.getFile(sshConfig.getSshKey()).canRead())) {
                throw new UtilityException("Provided keyfile cannot be accessed.");
            }

            switch (sshConfig.getSshPassword().length()) {
            case 0:
                jsch.addIdentity(FileUtils.getFile(sshConfig.getSshKey()).toString());

                break;
            default:
                jsch.addIdentity(FileUtils.getFile(sshConfig.getSshKey()).toString(),
                        PasswordUtils.decryptText(sshConfig.getSshPassword(), sshConfig.getSshSalt(),
                                secBean.getConfigData().getSecurityConfig().getEncryptionAlgorithm(),
                                secBean.getConfigData().getSecurityConfig().getIterations(),
                                secBean.getConfigData().getSecurityConfig().getKeyBits(),
                                secBean.getConfigData().getSecurityConfig().getEncryptionAlgorithm(),
                                secBean.getConfigData().getSecurityConfig().getEncryptionInstance(),
                                appBean.getConfigData().getSystemConfig().getEncoding()));

                break;
            }
        } else {
            session.setPassword(PasswordUtils.decryptText(sshConfig.getSshPassword(), sshConfig.getSshSalt(),
                    secBean.getConfigData().getSecurityConfig().getEncryptionAlgorithm(),
                    secBean.getConfigData().getSecurityConfig().getIterations(),
                    secBean.getConfigData().getSecurityConfig().getKeyBits(),
                    secBean.getConfigData().getSecurityConfig().getEncryptionAlgorithm(),
                    secBean.getConfigData().getSecurityConfig().getEncryptionInstance(),
                    appBean.getConfigData().getSystemConfig().getEncoding()));
        }

        session.connect((int) TimeUnit.SECONDS.toMillis(appBean.getConfigData().getSshConfig().getTimeout()));

        if (!(session.isConnected())) {
            throw new UtilityException("Failed to connect to the target host");
        }

        if (StringUtils.isNotEmpty(commandList)) {
            for (String cmd : StringUtils.split(commandList, ",")) {
                if (DEBUG) {
                    DEBUGGER.debug("cmd: {}", cmd);
                }

                channel = session.openChannel("exec");
                ((ChannelExec) channel).setCommand(cmd.trim());
                ((ChannelExec) channel).setErrStream(System.err);
                channel.setInputStream(null);

                if (DEBUG) {
                    DEBUGGER.debug("ChannelExec: {}", channel);
                }

                channel.connect(
                        (int) TimeUnit.SECONDS.toMillis(appBean.getConfigData().getSshConfig().getTimeout()));

                if (!(channel.isConnected())) {
                    throw new UtilityException("Failed to open a channel connection to the target host");
                }

                String line = null;
                BufferedReader bReader = new BufferedReader(new InputStreamReader(channel.getInputStream()));

                if (DEBUG) {
                    DEBUGGER.debug("BufferedReader: {}", bReader);
                }

                sBuilder = new StringBuilder();

                while ((line = bReader.readLine()) != null) {
                    if (DEBUG) {
                        DEBUGGER.debug("Data: {}", line);
                    }

                    sBuilder.append(line + CoreServiceConstants.LINE_BREAK);
                }

                if (DEBUG) {
                    DEBUGGER.debug("StringBuilder: {}", sBuilder.toString());
                }

                bReader.close();

                channel.disconnect();
            }
        }
    } catch (IOException iox) {
        throw new UtilityException(iox.getMessage(), iox);
    } catch (JSchException jx) {
        throw new UtilityException(jx.getMessage(), jx);
    } finally {
        if ((channel != null) && (channel.isConnected())) {
            channel.disconnect();
        }

        if ((session != null) && (session.isConnected())) {
            session.disconnect();
        }
    }

    return sBuilder;
}

From source file:algorithm.ZipPackagingTest.java

@Test
public void zipPackagingTest() {
    try {//from  ww  w  .ja va  2 s.  com
        File carrier = TestDataProvider.TXT_FILE;
        File payload1 = TestDataProvider.TXT_FILE_2;
        File payload2 = TestDataProvider.XML_FILE;
        List<File> payloadList = new ArrayList<File>();
        payloadList.add(payload1);
        payloadList.add(payload2);

        ZipPackaging algorithm = new ZipPackaging();
        // Test encapsulation:
        File outputFile = algorithm.encapsulate(carrier, payloadList);
        assertNotNull(outputFile);
        // Test restore:
        Hashtable<String, RestoredFile> outputHash = new Hashtable<String, RestoredFile>();
        for (RestoredFile file : algorithm.restore(outputFile)) {
            outputHash.put(file.getName(), file);
        }
        assertEquals(3, outputHash.size());
        RestoredFile restoredCarrier = outputHash.get(carrier.getName());
        RestoredFile restoredPayload1 = outputHash.get(payload1.getName());
        RestoredFile restoredPayload2 = outputHash.get(payload2.getName());
        assertNotNull(restoredCarrier);
        assertNotNull(restoredPayload1);
        assertNotNull(restoredPayload2);
        assertEquals(FileUtils.checksumCRC32(carrier), FileUtils.checksumCRC32(restoredCarrier));
        assertEquals(FileUtils.checksumCRC32(payload1), FileUtils.checksumCRC32(restoredPayload1));
        assertEquals(FileUtils.checksumCRC32(payload2), FileUtils.checksumCRC32(restoredPayload2));

        // check restoration metadata:
        // (algorithm doesn't save original file paths)
        assertEquals(algorithm, restoredCarrier.algorithm);
        assertTrue(restoredCarrier.checksumValid);
        assertTrue(restoredPayload1.checksumValid);
        assertTrue(restoredPayload2.checksumValid);
        // every file in a zip package is payload!:
        assertTrue(restoredCarrier.wasPayload);
        assertFalse(restoredCarrier.wasCarrier);
        assertTrue(restoredPayload1.wasPayload);
        assertFalse(restoredPayload1.wasCarrier);
        assertTrue(restoredPayload2.wasPayload);
        assertFalse(restoredPayload2.wasCarrier);
        assertTrue(restoredCarrier.relatedFiles.contains(restoredPayload1));
        assertTrue(restoredCarrier.relatedFiles.contains(restoredPayload2));
        assertFalse(restoredCarrier.relatedFiles.contains(restoredCarrier));
        assertTrue(restoredPayload1.relatedFiles.contains(restoredCarrier));
        assertTrue(restoredPayload1.relatedFiles.contains(restoredPayload2));
        assertFalse(restoredPayload1.relatedFiles.contains(restoredPayload1));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:algorithm.BagItPackagingTest.java

@Test
public void bagItPackagingTest() {
    try {//  ww  w  .j  av  a 2  s .  c o  m
        File carrier = TestDataProvider.TXT_FILE;
        File payload1 = TestDataProvider.TXT_FILE_2;
        File payload2 = TestDataProvider.XML_FILE;
        List<File> payloadList = new ArrayList<File>();
        payloadList.add(payload1);
        payloadList.add(payload2);
        BagItPackaging algorithm = new BagItPackaging();
        // Test encapsulation:
        File outputFile = algorithm.encapsulate(carrier, payloadList);
        assertNotNull(outputFile);
        assertTrue(outputFile.length() > carrier.length());

        // Test restore:
        Hashtable<String, RestoredFile> outputHash = new Hashtable<String, RestoredFile>();
        for (RestoredFile file : algorithm.restore(outputFile)) {
            outputHash.put(file.getName(), file);
        }
        assertEquals(3, outputHash.size());
        RestoredFile restoredCarrier = outputHash.get(carrier.getName());
        RestoredFile restoredPayload1 = outputHash.get(payload1.getName());
        RestoredFile restoredPayload2 = outputHash.get(payload2.getName());
        assertNotNull(restoredCarrier);
        assertNotNull(restoredPayload1);
        assertNotNull(restoredPayload2);

        assertEquals(FileUtils.checksumCRC32(carrier), FileUtils.checksumCRC32(restoredCarrier));
        assertEquals(FileUtils.checksumCRC32(payload1), FileUtils.checksumCRC32(restoredPayload1));
        assertEquals(FileUtils.checksumCRC32(payload2), FileUtils.checksumCRC32(restoredPayload2));

        // check restoration metadata:
        // (original file paths are not stored with BagIt)
        assertEquals(algorithm, restoredCarrier.algorithm);
        assertTrue(restoredCarrier.checksumValid);
        assertTrue(restoredPayload1.checksumValid);
        assertTrue(restoredPayload2.checksumValid);
        assertTrue(restoredCarrier.wasCarrier);
        assertFalse(restoredCarrier.wasPayload);
        assertTrue(restoredPayload1.wasPayload);
        assertFalse(restoredPayload1.wasCarrier);
        assertTrue(restoredPayload2.wasPayload);
        assertFalse(restoredPayload2.wasCarrier);
        assertTrue(restoredCarrier.relatedFiles.contains(restoredPayload1));
        assertTrue(restoredCarrier.relatedFiles.contains(restoredPayload2));
        assertFalse(restoredCarrier.relatedFiles.contains(restoredCarrier));
        assertTrue(restoredPayload1.relatedFiles.contains(restoredCarrier));
        assertTrue(restoredPayload1.relatedFiles.contains(restoredPayload2));
        assertFalse(restoredPayload1.relatedFiles.contains(restoredPayload1));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:algorithm.MetsSubmissionInformationPackageTest.java

@Test
public void MetsSipTest() {
    try {/*from w ww.  java 2s . c o  m*/
        File carrier = TestDataProvider.TXT_FILE;
        File payload1 = TestDataProvider.TXT_FILE_2;
        File payload2 = TestDataProvider.XML_FILE;
        List<File> payloadList = new ArrayList<File>();
        payloadList.add(payload1);
        payloadList.add(payload2);

        MetsSubmissionInformationPackage algorithm = new MetsSubmissionInformationPackage();
        // Test encapsulation:
        File outputFile = algorithm.encapsulate(carrier, payloadList);
        assertNotNull(outputFile);
        assertTrue(outputFile.length() > carrier.length());

        // Test restore:
        Hashtable<String, RestoredFile> outputHash = new Hashtable<String, RestoredFile>();
        for (RestoredFile file : algorithm.restore(outputFile)) {
            outputHash.put(file.getName(), file);
        }
        assertEquals(4, outputHash.size());// files+map!
        RestoredFile restoredCarrier = outputHash.get(carrier.getName());
        RestoredFile restoredPayload1 = outputHash.get(payload1.getName());
        RestoredFile restoredPayload2 = outputHash.get(payload2.getName());
        assertNotNull(restoredCarrier);
        assertNotNull(restoredPayload1);
        assertNotNull(restoredPayload2);
        assertEquals(FileUtils.checksumCRC32(carrier), FileUtils.checksumCRC32(restoredCarrier));
        assertEquals(FileUtils.checksumCRC32(payload1), FileUtils.checksumCRC32(restoredPayload1));
        assertEquals(FileUtils.checksumCRC32(payload2), FileUtils.checksumCRC32(restoredPayload2));

        // check restoration metadata:
        assertEquals(algorithm, restoredCarrier.algorithm);
        assertTrue(restoredCarrier.checksumValid);
        assertTrue(restoredPayload1.checksumValid);
        assertTrue(restoredPayload2.checksumValid);
        // every file in a zip package is payload!:
        assertTrue(restoredCarrier.wasPayload);
        assertFalse(restoredCarrier.wasCarrier);
        assertTrue(restoredPayload1.wasPayload);
        assertFalse(restoredPayload1.wasCarrier);
        assertTrue(restoredPayload2.wasPayload);
        assertFalse(restoredPayload2.wasCarrier);
        assertTrue(restoredCarrier.relatedFiles.contains(restoredPayload1));
        assertTrue(restoredCarrier.relatedFiles.contains(restoredPayload2));
        assertFalse(restoredCarrier.relatedFiles.contains(restoredCarrier));
        assertTrue(restoredPayload1.relatedFiles.contains(restoredCarrier));
        assertTrue(restoredPayload1.relatedFiles.contains(restoredPayload2));
        assertFalse(restoredPayload1.relatedFiles.contains(restoredPayload1));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:algorithm.OaiOreSubmissionInformationPackageTest.java

@Test
public void OaiOreSipTest() {
    try {//  w w  w  .  ja v  a 2  s .  co  m
        File carrier = TestDataProvider.TXT_FILE;
        File payload1 = TestDataProvider.TXT_FILE_2;
        File payload2 = TestDataProvider.XML_FILE;
        List<File> payloadList = new ArrayList<File>();
        payloadList.add(payload1);
        payloadList.add(payload2);

        OaiOreSubmissionInformationPackage algorithm = new OaiOreSubmissionInformationPackage();
        // Test encapsulation:
        File outputFile = algorithm.encapsulate(carrier, payloadList);
        assertNotNull(outputFile);
        assertTrue(outputFile.length() > carrier.length());

        // Test restore:
        Hashtable<String, RestoredFile> outputHash = new Hashtable<String, RestoredFile>();
        for (RestoredFile file : algorithm.restore(outputFile)) {
            outputHash.put(file.getName(), file);
        }
        assertEquals(4, outputHash.size());// files+map!
        RestoredFile restoredCarrier = outputHash.get(carrier.getName());
        RestoredFile restoredPayload1 = outputHash.get(payload1.getName());
        RestoredFile restoredPayload2 = outputHash.get(payload2.getName());
        assertNotNull(restoredCarrier);
        assertNotNull(restoredPayload1);
        assertNotNull(restoredPayload2);
        assertEquals(FileUtils.checksumCRC32(carrier), FileUtils.checksumCRC32(restoredCarrier));
        assertEquals(FileUtils.checksumCRC32(payload1), FileUtils.checksumCRC32(restoredPayload1));
        assertEquals(FileUtils.checksumCRC32(payload2), FileUtils.checksumCRC32(restoredPayload2));

        // check restoration metadata:
        assertEquals(algorithm, restoredCarrier.algorithm);
        assertTrue(restoredCarrier.checksumValid);
        assertTrue(restoredPayload1.checksumValid);
        assertTrue(restoredPayload2.checksumValid);
        // every file in a zip package is payload!:
        assertTrue(restoredCarrier.wasPayload);
        assertFalse(restoredCarrier.wasCarrier);
        assertTrue(restoredPayload1.wasPayload);
        assertFalse(restoredPayload1.wasCarrier);
        assertTrue(restoredPayload2.wasPayload);
        assertFalse(restoredPayload2.wasCarrier);
        assertTrue(restoredCarrier.relatedFiles.contains(restoredPayload1));
        assertTrue(restoredCarrier.relatedFiles.contains(restoredPayload2));
        assertFalse(restoredCarrier.relatedFiles.contains(restoredCarrier));
        assertTrue(restoredPayload1.relatedFiles.contains(restoredCarrier));
        assertTrue(restoredPayload1.relatedFiles.contains(restoredPayload2));
        assertFalse(restoredPayload1.relatedFiles.contains(restoredPayload1));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:algorithm.PNGChunkAddingTest.java

@Test
public void pngMultipleChunkAddingTest() {
    try {/*from w w  w. ja v  a 2  s . co m*/
        File carrier = TestDataProvider.PNG_FILE;
        File payload = TestDataProvider.TXT_FILE;
        File payload2 = TestDataProvider.TXT_FILE_2;

        List<File> payloadList = new ArrayList<File>();
        payloadList.add(payload);
        payloadList.add(payload2);

        PNGChunkAdding algorithm = new PNGChunkAdding();
        // Test encapsulation:
        File outputFile = algorithm.encapsulate(carrier, payloadList);
        assertNotNull(outputFile);

        // Test restore:
        Hashtable<String, RestoredFile> outputHash = new Hashtable<String, RestoredFile>();
        for (RestoredFile file : algorithm.restore(outputFile)) {
            outputHash.put(file.getName(), file);
        }
        assertEquals(3, outputHash.size());
        RestoredFile restoredCarrier = outputHash.get(carrier.getName());
        RestoredFile restoredPayload = outputHash.get(payload.getName());
        RestoredFile restoredPayload2 = outputHash.get(payload2.getName());
        assertNotNull(restoredCarrier);
        assertNotNull(restoredPayload);
        assertNotNull(restoredPayload2);
        // TODO: The carrier checksum is not valid, because the PNG library
        // reorders the chunks! We would have to use a different library.
        // assertEquals(FileUtils.checksumCRC32(carrier),
        // FileUtils.checksumCRC32(restoredCarrier));
        assertEquals(FileUtils.checksumCRC32(payload), FileUtils.checksumCRC32(restoredPayload));
        assertEquals(FileUtils.checksumCRC32(payload2), FileUtils.checksumCRC32(restoredPayload2));

        // check restoration metadata:
        assertEquals("" + carrier.getAbsolutePath(), restoredCarrier.originalFilePath);
        assertEquals("" + payload.getAbsolutePath(), restoredPayload.originalFilePath);
        assertEquals(algorithm, restoredCarrier.algorithm);
        // can't be true for this algorithm:
        // assertTrue(restoredCarrier.checksumValid);
        assertTrue(restoredPayload.checksumValid);
        assertTrue(restoredCarrier.wasCarrier);
        assertFalse(restoredCarrier.wasPayload);
        assertTrue(restoredPayload.wasPayload);
        assertFalse(restoredPayload.wasCarrier);
        assertTrue(restoredCarrier.relatedFiles.contains(restoredPayload));
        assertFalse(restoredCarrier.relatedFiles.contains(restoredCarrier));
        assertTrue(restoredPayload.relatedFiles.contains(restoredCarrier));
        assertFalse(restoredPayload.relatedFiles.contains(restoredPayload));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:edu.ku.brc.ui.skin.SkinsMgr.java

/**
 * Loads the skins from XML./*from   w  w w .jav a2 s.  c  o  m*/
 */
@SuppressWarnings("unchecked")
protected static Hashtable<String, Skin> load() {
    XStream xstream = new XStream();
    config(xstream);

    if (false) {
        Hashtable<String, Skin> skinsHash = new Hashtable<String, Skin>();

        /*
         giraffe_tile.png
        giraffe_tile1.png
        metal.jpg
        reptile_tiled.jpg
        reptile_tiled.png
         */
        String p = "/Users/rods/workspace/Specify6/";
        Skin skin1 = new Skin("metal", "metal desc");
        skin1.getItems().put("item1", new SkinItem("n1", "fn", 10, "plain", Color.BLACK, Color.MAGENTA,
                p + "giraffe_tile.png", "desc"));
        skin1.getItems().put("item2", new SkinItem("n2", "fn", 10, "bold", Color.BLACK, Color.ORANGE,
                p + "giraffe_tile.png", "desc"));

        Skin skin2 = new Skin("giraffe", "metal desc");
        skin2.getItems().put("item1",
                new SkinItem("n1", "fn", 10, "bolditalic", Color.GREEN, Color.YELLOW, p + "metal.jpg", "desc"));
        skin2.getItems().put("item2",
                new SkinItem("n2", "fn", 10, "italic", Color.BLUE, Color.RED, p + "metal.jpg", "desc"));

        skinsHash.put(skin1.getName(), skin1);
        skinsHash.put(skin2.getName(), skin2);

        File skinsFile = new File(XMLHelper.getConfigDirPath("skins.xml"));
        try {
            FileUtils.writeStringToFile(skinsFile, xstream.toXML(skinsHash));
        } catch (IOException ex) {
            edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
            edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(SkinsMgr.class, ex);
            ex.printStackTrace();
        }
    }

    File skinsFile = new File(XMLHelper.getConfigDirPath("skins.xml"));
    if (skinsFile.exists()) {
        return (Hashtable<String, Skin>) xstream.fromXML(XMLHelper.getContents(skinsFile));
    }
    return null;
}

From source file:com.archivas.clienttools.arcutils.impl.adapter.Hcp6AuthNamespaceAdapter.java

private boolean handleAdditionalMetadata(FileMetadata metadata, final String annotations,
        final String filePath) {
    if (!annotations.isEmpty()) {
        Hashtable<String, CustomMetadata> aList = new Hashtable<String, CustomMetadata>();
        for (String a : annotations.split(", ")) {
            String[] tmp = a.split(";");
            aList.put(tmp[0], new CustomMetadata(CustomMetadata.Form.PROFILED, filePath, null, tmp[0]));
        }/*from  w  w w  .j  a v a 2 s.c o m*/
        metadata.setAnnotationList(aList);
    }
    return true;
}

From source file:algorithm.TxtInformationFrameTest.java

@Test
public void textInformationFrameAlgorithmTest() {
    try {/*from   ww  w  . ja v  a 2  s.c o m*/
        File carrier = TestDataProvider.TXT_FILE;
        File payload1 = TestDataProvider.TXT_FILE_2;
        File payload2 = TestDataProvider.XML_FILE;
        List<File> payloadList = new ArrayList<File>();
        payloadList.add(payload1);
        payloadList.add(payload2);

        TextInformationFrame algorithm = new TextInformationFrame();
        // Test encapsulation:
        File outputFile = algorithm.encapsulate(carrier, payloadList);
        assertNotNull(outputFile);
        assertTrue(outputFile.length() > carrier.length());
        // Test restore:
        Hashtable<String, RestoredFile> outputHash = new Hashtable<String, RestoredFile>();
        for (RestoredFile file : algorithm.restore(outputFile)) {
            outputHash.put(file.getName(), file);
        }
        assertEquals(3, outputHash.size());
        RestoredFile restoredCarrier = outputHash.get(carrier.getName());
        RestoredFile restoredPayload1 = outputHash.get(payload1.getName());
        RestoredFile restoredPayload2 = outputHash.get(payload2.getName());
        assertNotNull(restoredCarrier);
        assertNotNull(restoredPayload1);
        assertNotNull(restoredPayload2);
        assertEquals(FileUtils.checksumCRC32(carrier), FileUtils.checksumCRC32(restoredCarrier));
        assertEquals(FileUtils.checksumCRC32(payload1), FileUtils.checksumCRC32(restoredPayload1));
        assertEquals(FileUtils.checksumCRC32(payload2), FileUtils.checksumCRC32(restoredPayload2));

        // check restoration metadata:
        assertEquals("" + carrier.getAbsolutePath(), restoredCarrier.originalFilePath);
        assertEquals("" + payload1.getAbsolutePath(), restoredPayload1.originalFilePath);
        assertEquals("" + payload2.getAbsolutePath(), restoredPayload2.originalFilePath);
        assertEquals(algorithm, restoredCarrier.algorithm);
        assertTrue(restoredCarrier.checksumValid);
        assertTrue(restoredPayload1.checksumValid);
        assertTrue(restoredPayload2.checksumValid);
        assertTrue(restoredCarrier.wasCarrier);
        assertFalse(restoredCarrier.wasPayload);
        assertTrue(restoredPayload1.wasPayload);
        assertFalse(restoredPayload1.wasCarrier);
        assertTrue(restoredPayload2.wasPayload);
        assertFalse(restoredPayload2.wasCarrier);
        assertTrue(restoredCarrier.relatedFiles.contains(restoredPayload1));
        assertTrue(restoredCarrier.relatedFiles.contains(restoredPayload2));
        assertFalse(restoredCarrier.relatedFiles.contains(restoredCarrier));
        assertTrue(restoredPayload1.relatedFiles.contains(restoredCarrier));
        assertTrue(restoredPayload1.relatedFiles.contains(restoredPayload2));
        assertFalse(restoredPayload1.relatedFiles.contains(restoredPayload1));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.nokia.helium.metadata.DerbyFactoryManagerCreator.java

public synchronized EntityManagerFactory create(File database) throws MetadataException {
    EntityManagerFactory factory;/*from w ww. j  a  va2s .  co  m*/
    String name = "metadata";
    Hashtable<String, String> persistProperties = new Hashtable<String, String>();
    persistProperties.put("javax.persistence.jdbc.driver", "org.apache.derby.jdbc.EmbeddedDriver");
    // This swallow all the output log from derby.
    System.setProperty("derby.stream.error.field",
            "com.nokia.helium.metadata.DerbyFactoryManagerCreator.DEV_NULL");
    persistProperties.put("javax.persistence.jdbc.url", "jdbc:derby:" + database.getAbsolutePath());
    persistProperties.put(PersistenceUnitProperties.PERSISTENCE_CONTEXT_CLOSE_ON_COMMIT, "false");
    persistProperties.put(PersistenceUnitProperties.PERSISTENCE_CONTEXT_REFERENCE_MODE, "WEAK");
    persistProperties.put(PersistenceUnitProperties.BATCH_WRITING, "JDBC");
    persistProperties.put("eclipselink.read-only", "true");
    persistProperties.put(PersistenceUnitProperties.LOGGING_LEVEL, "warning");
    if (database.exists()) {
        if (!checkDatabaseIntegrity(database)) {
            try {
                FileUtils.forceDelete(database);
            } catch (java.io.IOException iex) {
                throw new MetadataException("Failed deleting corrupted db: " + iex, iex);
            }
        } else {
            return Persistence.createEntityManagerFactory(name, persistProperties);
        }
    }
    persistProperties.put("javax.persistence.jdbc.url", "jdbc:derby:" + database + ";create=true");
    persistProperties.put(PersistenceUnitProperties.DDL_GENERATION, "create-tables");
    persistProperties.put(PersistenceUnitProperties.DDL_GENERATION_MODE, "database");
    persistProperties.put(PersistenceUnitProperties.PERSISTENCE_CONTEXT_CLOSE_ON_COMMIT, "false");
    persistProperties.put(PersistenceUnitProperties.PERSISTENCE_CONTEXT_REFERENCE_MODE, "WEAK");
    persistProperties.put(PersistenceUnitProperties.BATCH_WRITING, "JDBC");
    persistProperties.put("eclipselink.read-only", "true");
    factory = Persistence.createEntityManagerFactory(name, persistProperties);
    EntityManager entityManager = factory.createEntityManager();
    // Pushing default data into the current schema
    try {
        entityManager.getTransaction().begin();
        // Version of the schema is pushed.
        entityManager.persist(new Version());
        // Default set of severity is pushed.
        for (SeverityEnum.Severity severity : SeverityEnum.Severity.values()) {
            Severity pData = new Severity();
            pData.setSeverity(severity.toString());
            entityManager.persist(pData);
        }
        entityManager.getTransaction().commit();
    } finally {
        if (entityManager.getTransaction().isActive()) {
            entityManager.getTransaction().rollback();
            entityManager.clear();
        }
        entityManager.close();
    }
    return factory;
}