alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Glassfish example source code file (OneWork.java)

This example Glassfish source code file (OneWork.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Glassfish tags/keywords

classloader, classloader, log, logging, object, object, onework, string, string, stringbuffer, stringmanager, throwable, work, work, workcontexthandler, workcoordinator

The Glassfish OneWork.java source code

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved.
 *
 * The contents of this file are subject to the terms of either the GNU
 * General Public License Version 2 only ("GPL") or the Common Development
 * and Distribution License("CDDL") (collectively, the "License").  You
 * may not use this file except in compliance with the License.  You can
 * obtain a copy of the License at
 * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
 * or packager/legal/LICENSE.txt.  See the License for the specific
 * language governing permissions and limitations under the License.
 *
 * When distributing the software, include this License Header Notice in each
 * file and include the License file at packager/legal/LICENSE.txt.
 *
 * GPL Classpath Exception:
 * Oracle designates this particular file as subject to the "Classpath"
 * exception as provided by Oracle in the GPL Version 2 section of the License
 * file that accompanied this code.
 *
 * Modifications:
 * If applicable, add the following below the License Header, with the fields
 * enclosed by brackets [] replaced by your own identifying information:
 * "Portions Copyright [year] [name of copyright owner]"
 *
 * Contributor(s):
 * If you wish your version of this file to be governed by only the CDDL or
 * only the GPL Version 2, indicate your decision by adding "[Contributor]
 * elects to include this software in this distribution under the [CDDL or GPL
 * Version 2] license."  If you don't indicate a single choice of license, a
 * recipient has the option to distribute your version of this file under
 * either the CDDL, the GPL Version 2 or to extend the choice of license to
 * its licensees as provided above.  However, if you add GPL Version 2 code
 * and therefore, elected the GPL Version 2 license, then the option applies
 * only if the new code is made subject to such option by the copyright
 * holder.
 */

package com.sun.enterprise.connectors.work;

import com.sun.enterprise.util.i18n.StringManager;
import com.sun.logging.LogDomains;

import javax.resource.spi.work.Work;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.sun.enterprise.connectors.work.context.WorkContextHandler;

/**
 * Represents one piece of work that will be submitted to the workqueue.
 *
 * @author Binod P.G
 */
public final class OneWork implements com.sun.corba.ee.spi.orbutil.threadpool.Work {

    private final Work work;
    private final WorkCoordinator coordinator;
    private long nqTime;
    private WorkContextHandler contextHandler;
    private static final Logger logger =
            LogDomains.getLogger(OneWork.class, LogDomains.RSR_LOGGER);
    private StringManager localStrings =
            StringManager.getManager(OneWork.class);

    private String name = "Resource adapter work";
    private boolean nameSet = false;
    
    //Store the client's TCC so that when the work is executed,
    //TCC is set appropriately.
    private ClassLoader tcc = null;

    /**
     * Creates a work object that can be submitted to a workqueue.
     *
     * @param work Actual work submitted by Resource adapter.
     * @param coordinator <code>WorkCoordinator object.
     */
    OneWork (Work work, WorkCoordinator coordinator, WorkContextHandler contextHandler, ClassLoader tcc) {
        this.work = work;
        this.coordinator = coordinator;
        this.contextHandler = contextHandler;
        this.tcc = tcc;
    }

    /**
     * This method is executed by thread pool as the basic work operation.
     */
    public void doWork() {
        ClassLoader callerCL = Thread.currentThread().getContextClassLoader();
        if(tcc != null && tcc != callerCL){
            Thread.currentThread().setContextClassLoader(tcc);
        }
        try{
        coordinator.preInvoke(); // pre-invoke will set work state to "started",
        boolean timedOut = coordinator.isTimedOut();

        // validation of work context should be after this
        //so as to throw WorkCompletedException in case of error.
        if (coordinator.proceed()) {
            try {
                coordinator.setupContext(this);
                //work-name will be set (if specified via HintsContext "javax.resources.spi.HintsContext.NAME_HINT")
                log("Start of Work");
            } catch (Throwable e) {
                coordinator.setException(e);
            }
        }

        //there may be failure in context setup
        if(coordinator.proceed()){
            try {
                work.run();
                log("Work Executed");
            } catch (Throwable t) {
                log("Execution has thrown exception " + t.getMessage());
                coordinator.setException(t);
            }
        }

        if(!timedOut){
            coordinator.postInvoke();
        }
        log("End of Work");
        }finally{
            ClassLoader cl = Thread.currentThread().getContextClassLoader();
            if(cl != callerCL){
                Thread.currentThread().setContextClassLoader(callerCL);
            }
            tcc = null;
        }
    }

    public void log(String message){
        if(nameSet){
            Object args[] = new Object[]{name, message};
            String msg = localStrings.getString("log.work.hint", args);
            logger.log(Level.INFO,msg);
        }
    }

    /**
     * Time at which this work is enqueued.
     *
     * @param tme Time in milliseconds.
     */
    public void setEnqueueTime(long tme) {
        this.nqTime = tme;
    }

    /**
     * Retrieves the time at which this work is enqueued
     *
     * @return Time in milliseconds.
     */
    public long getEnqueueTime() {
        return nqTime;
    }

    /**
     * Retrieves the name of the work.
     *
     * @return Name of the work.
     */
    public String getName() {
        return name;
    }

    public void setName(String name){
        this.name = name;
        nameSet = true;
    }

    /**
     * Retrieves the string representation of work.
     *
     * @return String representation of work.
     */
    public String toString() {
        StringBuffer result = new StringBuffer();
        if(nameSet){
            result.append("[Work : " + name + "] ");
        }
        result.append(work.toString());
        return result.toString();
    }
}

Other Glassfish examples (source code examples)

Here is a short list of links related to this Glassfish OneWork.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.