Example usage for javax.naming StringRefAddr getContent

List of usage examples for javax.naming StringRefAddr getContent

Introduction

In this page you can find the example usage for javax.naming StringRefAddr getContent.

Prototype

public Object getContent() 

Source Link

Document

Retrieves the contents of this address.

Usage

From source file:org.pepstock.jem.jppf.ExecuteManager.java

/**
 * Create a JPPF job that can be submitted for execution.
 * //from w w w .  j  av  a  2 s.  c  o m
 * @param runnable Class name of Runnable or JPPFTask to execute
 * @param parallelTaskNumber Number of parallel task to submit
 * @param xmlContext InitialContext, serialized in XML string
 * @return an instance of the {@link org.jppf.client.JPPFJob JPPFJob} class.
 * @throws NamingException 
 * @throws IOException 
 * @throws JPPFException 
 * @throws JPFFException
 *             if an error occurs while creating the job or adding tasks.
 */
private static JPPFJob createJob(String xmlContext)
        throws JPPFMessageException, NamingException, IOException, JPPFException {
    XStream xstream = new XStream();
    // reads all properties
    String jobName = JPPFConfiguration.getProperties().getProperty(Keys.JEM_JOB_NAME);
    String taskName = JPPFConfiguration.getProperties().getProperty(Keys.JEM_TASK_NAME);
    String runnable = JPPFConfiguration.getProperties().getProperty(Keys.JEM_RUNNABLE);
    int parallelTaskNumber = JPPFConfiguration.getProperties().getInt(Keys.JEM_TASK_NUMBER);

    // creates a data provider to pass initial context and number of parallel task
    MemoryMapDataProvider provider = new MemoryMapDataProvider();
    provider.setValue(Keys.JEM_CONTEXT, xmlContext);
    provider.setValue(Keys.JEM_TASK_NUMBER, String.valueOf(parallelTaskNumber));

    // checks if CHUNK is set
    if (JPPFConfiguration.getProperties().containsKey(Keys.JEM_CHUNKABLE_DATA_DESCRIPTION)) {
        // gets data description
        String dataDescription = JPPFConfiguration.getProperties()
                .getProperty(Keys.JEM_CHUNKABLE_DATA_DESCRIPTION);
        File file = null;
        InitialContext ic = ContextUtils.getContext();

        // lookup for JNDI reference
        // needs file name because chunk is based on RandomFileAccess
        // that wants a FILE and not inputstream
        // so gets data description implementation object
        // to get REAL FILE
        NamingEnumeration<NameClassPair> list = ic.list(dataDescription);
        while (list.hasMore()) {
            NameClassPair pair = list.next();
            // checks if is datastream
            // only datastreams are searched            
            if (pair.getName().equalsIgnoreCase(dataDescription) && pair instanceof DataStreamNameClassPair) {
                DataStreamNameClassPair dsPair = (DataStreamNameClassPair) pair;
                DataStreamReference prevReference = (DataStreamReference) dsPair.getObject();
                // gets data description XML definition
                StringRefAddr sra = (StringRefAddr) prevReference.get(StringRefAddrKeys.DATASTREAMS_KEY);
                // creates datadescription implementatio to get file 
                DataDescriptionImpl ddImpl = (DataDescriptionImpl) xstream.fromXML(sra.getContent().toString());
                file = ddImpl.getDatasets().iterator().next().getRealFile();
                // leaves while
                break;
            }
        }
        // if file is null
        // data description is not found
        if (file == null) {
            throw new JPPFMessageException(JPPFMessage.JEMJ019E, dataDescription);
        }
        // calculated buffersize
        long bufferSize = file.length() / parallelTaskNumber;

        // using delimiter, creates chunk list
        List<ChunkDefinition> chunks = null;
        String delimiter = JPPFConfiguration.getProperties().getProperty(Keys.JEM_DELIMITER);
        String delimiterString = JPPFConfiguration.getProperties().getProperty(Keys.JEM_DELIMITER_STRING);

        if (delimiter != null) {
            // delimiter default LF
            char splitter = CharUtils.LF;
            // if delimiter is defined in BYTE mode
            // calculate char
            if (delimiter.toLowerCase().startsWith("0x")) {
                delimiter = StringUtils.substringAfter(delimiter.toLowerCase(), "0x");
                splitter = (char) Integer.parseInt(delimiter, 16);
            } else {
                // if uses a escape java char
                splitter = StringEscapeUtils.unescapeJava(delimiter).charAt(0);
            }
            String displayDelimiter = Integer.toHexString((byte) splitter) + "(" + splitter + ")";
            LogAppl.getInstance().emit(JPPFMessage.JEMJ020I, displayDelimiter);

            // calculates chunks
            chunks = ChunksFactory.getChunksByDelimiter(file, bufferSize, splitter);
            provider.setValue(Keys.JEM_DELIMITER, splitter);
        } else if (delimiterString != null) {
            LogAppl.getInstance().emit(JPPFMessage.JEMJ020I, delimiterString);
            // calculates chunks
            chunks = ChunksFactory.getChunksByDelimiter(file, bufferSize, delimiterString);
            provider.setValue(Keys.JEM_DELIMITER, delimiterString);
        } else {
            // if delimiter and delimiterString are missing, uses default delimiter (System.getProperty("line.separator")
            chunks = ChunksFactory.getChunksByDelimiter(file, bufferSize);
        }
        // changes parallel task
        // because chunk calculation tries to maintain the same 
        // number of task but usually there are less after
        // chunk list creation
        parallelTaskNumber = chunks.size();

        LogAppl.getInstance().emit(JPPFMessage.JEMJ021I, chunks);

        // serializes and saves on data provider all necessary data 
        provider.setValue(Keys.JEM_TASK_NUMBER, String.valueOf(parallelTaskNumber));
        provider.setValue(Keys.JEM_CHUNKABLE_DATA_DESCRIPTION, dataDescription);
        provider.setValue(Keys.JEM_CHUNKS, xstream.toXML(chunks));
    }

    LogAppl.getInstance().emit(JPPFMessage.JEMJ023I, parallelTaskNumber);

    // checks if merged data decritpion is an argument
    if (JPPFConfiguration.getProperties().containsKey(Keys.JEM_MERGED_DATA_DESCRIPTION)) {
        // loads a list with all temporary files for tasks
        // to use to write partial output
        for (int i = 0; i < parallelTaskNumber; i++) {
            File temp = File.createTempFile("task[" + i + "]-merge", ".jemjppf");
            temp.deleteOnExit();
            TEMPORARY_FILES.add(temp);
            LogAppl.getInstance().emit(JPPFMessage.JEMJ022I, temp.getAbsolutePath(), i);
        }
        // sets data provider
        provider.setValue(Keys.JEM_MERGED_DATA_DESCRIPTION,
                JPPFConfiguration.getProperties().getProperty(Keys.JEM_MERGED_DATA_DESCRIPTION));
        provider.setValue(Keys.JEM_TEMPORARY_FILES, xstream.toXML(TEMPORARY_FILES));
    }

    // create a JPPF job
    JPPFJob job = new JPPFJob(provider);

    // give this job a readable unique id that we can use to monitor and
    // manage it.
    job.setName(jobName + "." + taskName);

    // Checks what instance has been past to distribute on grid
    try {
        Object clazz = Class.forName(runnable).newInstance();
        if (clazz instanceof JPPFTask) {
            for (int i = 0; i < parallelTaskNumber; i++) {
                JPPFTask t = new WrapperJPPFTask((JPPFTask) clazz);
                // add a task to the job.
                job.addTask(t);
            }
        } else if (clazz instanceof Runnable) {
            for (int i = 0; i < parallelTaskNumber; i++) {
                JPPFTask t = new RunnableTask((Runnable) clazz);
                // add a task to the job.
                job.addTask(t);
            }
        } else {
            throw new JPPFMessageException(JPPFMessage.JEMJ002E, runnable, Runnable.class.getName(),
                    JPPFTask.class.getName());
        }
    } catch (InstantiationException e) {
        throw new JPPFMessageException(JPPFMessage.JEMJ002E, e, runnable, Runnable.class.getName(),
                JPPFTask.class.getName());
    } catch (IllegalAccessException e) {
        throw new JPPFMessageException(JPPFMessage.JEMJ002E, e, runnable, Runnable.class.getName(),
                JPPFTask.class.getName());
    } catch (ClassNotFoundException e) {
        throw new JPPFMessageException(JPPFMessage.JEMJ002E, e, runnable, Runnable.class.getName(),
                JPPFTask.class.getName());
    }

    // there is no guarantee on the order of execution of the tasks,
    // however the results are guaranteed to be returned in the same order
    // as the tasks.
    return job;
}