de.zib.gndms.dspace.subspace.service.globus.resource.ExtSubspaceResourceHome.java Source code

Java tutorial

Introduction

Here is the source code for de.zib.gndms.dspace.subspace.service.globus.resource.ExtSubspaceResourceHome.java

Source

package de.zib.gndms.dspace.subspace.service.globus.resource;

/*
 * Copyright 2008-2011 Zuse Institute Berlin (ZIB)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import de.zib.gndms.dspace.common.DSpaceTools;
import de.zib.gndms.dspace.service.globus.resource.ExtDSpaceResourceHome;
import de.zib.gndms.dspace.subspace.stubs.types.SubspaceReference;
import de.zib.gndms.infra.GNDMSTools;
import de.zib.gndms.infra.GridConfig;
import de.zib.gndms.infra.service.GNDMPersistentServiceHome;
import de.zib.gndms.infra.system.GNDMSystem;
import de.zib.gndms.infra.wsrf.ReloadablePersistentResource;
import de.zib.gndms.model.dspace.Subspace;
import org.apache.axis.message.addressing.AttributedURI;
import org.apache.axis.message.addressing.EndpointReferenceType;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.globus.wsrf.Resource;
import org.globus.wsrf.ResourceException;
import org.globus.wsrf.ResourceKey;
import org.globus.wsrf.impl.SimpleResourceKey;
import org.globus.wsrf.utils.AddressingUtils;
import org.jetbrains.annotations.NotNull;

import javax.naming.NamingException;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.xml.namespace.QName;

/**
 * This class overrides the ResourceHome that is automatically generated by introduce for Globus
 * Toolkit. In GNDMS this is mainly necessary to provide RDBMS/JPA-based resource persistence.
 * In order to use the extended resource home they have to be configured in jndi-config.xml.
 * If this has been done properly, you should see an info-level log message during the start up
 * of the web service container that notifies succesfull initialization of the extended resource
 * home.
 *
 * @author  try ste fan pla nti kow zib
 * @version $Id$
 *
 *          User: stepn Date: 16.07.2008 Time: 12:35:27
 */
public final class ExtSubspaceResourceHome extends SubspaceResourceHome
        implements GNDMPersistentServiceHome<Subspace> {

    // logger can be an instance field since resource home classes are instantiated at most once
    @NotNull
    @SuppressWarnings({ "FieldNameHidesFieldInSuperclass" })
    private final Log logger = LogFactory.getLog(ExtSubspaceResourceHome.class);

    private boolean initialized;

    // System: Set during initialization
    @SuppressWarnings({ "FieldAccessedSynchronizedAndUnsynchronized" })
    @NotNull
    private GNDMSystem system;

    // Serbice Address: set during initialization
    @SuppressWarnings({ "FieldAccessedSynchronizedAndUnsynchronized" })
    private AttributedURI serviceAddress;

    @Override
    public synchronized void initialize() throws Exception {
        if (!initialized) {
            logger.info("Extended Subspace home initializing");
            try {
                try {
                    final GridConfig gridConfig = ExtDSpaceResourceHome.getGridConfig();
                    logger.debug("Config: " + gridConfig.asString());
                    system = gridConfig.retrieveSystemReference();
                    serviceAddress = GNDMSTools.getServiceAddressFromContext();

                    initialized = true;

                    super.initialize(); // Overridden method
                } catch (NamingException e) {
                    throw new RuntimeException(e);
                }
            } catch (RuntimeException e) {
                initialized = false;
                logger.error("Initialization failed", e);
                e.printStackTrace(System.err);
                throw e;
            }
        }
    }

    private void ensureInitialized() {
        try {
            initialize();
        } catch (Exception e) {
            logger.error("Unexpected initialization error", e);
            throw new RuntimeException(e);
        }
    }

    @SuppressWarnings({ "unchecked", "RawUseOfParameterizedType" })
    @Override
    protected Resource createNewInstance() throws ResourceException {
        final Resource instance = super.createNewInstance();
        ((ReloadablePersistentResource) instance).setResourceHome(this);
        return instance;
    }

    @NotNull
    public AttributedURI getServiceAddress() {
        ensureInitialized();
        return serviceAddress;
    }

    @NotNull
    public final QName getResourceKeyTypeName() {
        return getKeyTypeName();
    }

    @NotNull
    public GNDMSystem getSystem() throws IllegalStateException {
        ensureInitialized();
        return system;
    }

    public void setSystem(@NotNull GNDMSystem systemParam) throws IllegalStateException {
        throw new UnsupportedOperationException("Cant overwrite system");
    }

    @NotNull
    public EntityManagerFactory getEntityManagerFactory() {
        return getSystem().getEntityManagerFactory();
    }

    @NotNull
    public Query getListAllQuery(final @NotNull EntityManager em) {
        return em.createNamedQuery("listAllSubspaceIds");
    }

    public void refresh(final @NotNull Subspace resourceModel) throws ResourceException {
        DSpaceTools.refreshModelResource(resourceModel, this);
    }

    @NotNull
    public String getNickName() {
        return "subspace";
    }

    @NotNull
    public Class<Subspace> getModelClass() {
        return Subspace.class;
    }

    public ResourceKey getKeyForResourceModel(@NotNull Subspace model) {
        return getKeyForId(model.getId());
    }

    @NotNull
    public ResourceKey getKeyForId(@NotNull String id) {
        return new SimpleResourceKey(getKeyTypeName(), id);
    }

    @NotNull
    public SubspaceReference getReferenceForSubspace(Subspace model) throws Exception {
        return getResourceReference(getKeyForId(model.getId()));
    }

    @Override
    public Resource find(final ResourceKey resourceKeyParam) throws ResourceException {
        logger.info("find: " + resourceKeyParam == null ? "null"
                : resourceKeyParam.getName() + "/"
                        + (resourceKeyParam.getValue() == null ? "null" : resourceKeyParam.getValue()));
        return super.find(resourceKeyParam); // Overridden method
    }

    @Override
    public void remove(final ResourceKey resourceKeyParam) throws ResourceException {
        logger.info("remove: " + resourceKeyParam == null ? "null"
                : resourceKeyParam.getName() + "/"
                        + (resourceKeyParam.getValue() == null ? "null" : resourceKeyParam.getValue()));
        super.remove(resourceKeyParam); // Overridden method
    }

    @Override
    protected void add(final ResourceKey resourceKeyParam, final Resource resourceParam) {
        logger.info("add: " + resourceKeyParam == null ? "null"
                : resourceKeyParam.getName() + "/"
                        + (resourceKeyParam.getValue() == null ? "null" : resourceKeyParam.getValue()));
        super.add(resourceKeyParam, resourceParam); // Overridden method
    }

    @Override
    public SubspaceReference getResourceReference(final @NotNull ResourceKey key) throws Exception {
        EndpointReferenceType epr = AddressingUtils.createEndpointReference(serviceAddress.toString(), key);
        de.zib.gndms.dspace.subspace.stubs.types.SubspaceReference ref = new de.zib.gndms.dspace.subspace.stubs.types.SubspaceReference();
        ref.setEndpointReference(epr);
        return ref;
    }
}