All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.glassfish.hk2.configuration.hub.internal.WriteableBeanDatabaseImpl Maven / Gradle / Ivy

There is a newer version: 4.0.0-M3
Show newest version
/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2014-2017 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 org.glassfish.hk2.configuration.hub.internal;

import java.io.PrintStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;

import org.glassfish.hk2.api.MultiException;
import org.glassfish.hk2.api.TwoPhaseResource;
import org.glassfish.hk2.api.TwoPhaseTransactionData;
import org.glassfish.hk2.configuration.hub.api.BeanDatabaseUpdateListener;
import org.glassfish.hk2.configuration.hub.api.Change;
import org.glassfish.hk2.configuration.hub.api.Instance;
import org.glassfish.hk2.configuration.hub.api.Type;
import org.glassfish.hk2.configuration.hub.api.WriteableBeanDatabase;
import org.glassfish.hk2.configuration.hub.api.WriteableType;

/**
 * @author jwells
 *
 */
public class WriteableBeanDatabaseImpl implements WriteableBeanDatabase {
    private final long baseRevision;
    private final HashMap types = new HashMap();
    private final HubImpl hub;
    private final TwoPhaseResourceImpl resource = new TwoPhaseResourceImpl();
    
    private final LinkedList changes = new LinkedList();
    private final LinkedList removedTypes = new LinkedList();
    private boolean committed = false;
    private Object commitMessage = null;
    
    /* package */ WriteableBeanDatabaseImpl(HubImpl hub, BeanDatabaseImpl currentDatabase) {
        this.hub = hub;
        baseRevision = currentDatabase.getRevision();
        
        for (Type type : currentDatabase.getAllTypes()) {
            types.put(type.getName(), new WriteableTypeImpl(this, (TypeImpl) type));
        }
        
    }

    /* (non-Javadoc)
     * @see org.glassfish.hk2.configuration.hub.api.BeanDatabase#getAllTypes()
     */
    @Override
    public synchronized Set getAllTypes() {
        return Collections.unmodifiableSet(new HashSet(types.values()));
    }
    
    /* (non-Javadoc)
     * @see org.glassfish.hk2.configuration.hub.api.WriteableBeanDatabase#getAllWriteableTypes()
     */
    @Override
    public Set getAllWriteableTypes() {
        return Collections.unmodifiableSet(new HashSet(types.values()));
    }

    /* (non-Javadoc)
     * @see org.glassfish.hk2.configuration.hub.api.BeanDatabase#getType(java.lang.String)
     */
    @Override
    public synchronized Type getType(String type) {
        return types.get(type);
    }
    
    /* (non-Javadoc)
     * @see org.glassfish.hk2.configuration.hub.api.BeanDatabase#getInstance(java.lang.String, java.lang.Object)
     */
    @Override
    public synchronized Instance getInstance(String type, String instanceKey) {
        Type t = getType(type);
        if (t == null) return null;
        
        return t.getInstance(instanceKey);
    }
    
    private void checkState() {
        if (committed) throw new IllegalStateException("This database has already been committed");
    }

    /* (non-Javadoc)
     * @see org.glassfish.hk2.configuration.hub.api.WriteableBeanDatabase#addType(java.lang.String)
     */
    @Override
    public synchronized WriteableType addType(String typeName) {
        if (typeName == null) throw new IllegalArgumentException();
        checkState();
        
        WriteableTypeImpl wti = new WriteableTypeImpl(this, typeName);
        
        changes.add(new ChangeImpl(Change.ChangeCategory.ADD_TYPE,
                                   wti,
                                   null,
                                   null,
                                   null,
                                   null));
        
        types.put(typeName, wti);
        
        return wti;
    }

    /* (non-Javadoc)
     * @see org.glassfish.hk2.configuration.hub.api.WriteableBeanDatabase#removeType(java.lang.String)
     */
    @Override
    public synchronized Type removeType(String typeName) {
        if (typeName == null) throw new IllegalArgumentException();
        checkState();
        
        WriteableTypeImpl retVal = types.remove(typeName);
        if (retVal == null) return null;
        
        Map instances = retVal.getInstances();
        for (String key : new HashSet(instances.keySet())) {
            retVal.removeInstance(key);
        }
        
        changes.add(new ChangeImpl(Change.ChangeCategory.REMOVE_TYPE,
                retVal,
                null,
                null,
                null,
                null));
        
        removedTypes.add(retVal);
        
        return retVal;
    }

    /* (non-Javadoc)
     * @see org.glassfish.hk2.configuration.hub.api.WriteableBeanDatabase#getWriteableType(java.lang.String)
     */
    @Override
    public synchronized WriteableType getWriteableType(String typeName) {
        checkState();
        return types.get(typeName);
    }

    /* (non-Javadoc)
     * @see org.glassfish.hk2.configuration.hub.api.WriteableBeanDatabase#findOrAddWriteableType(java.lang.String)
     */
    @Override
    public synchronized WriteableType findOrAddWriteableType(String typeName) {
        if (typeName == null) throw new IllegalArgumentException();
        checkState();
        
        WriteableTypeImpl wti = types.get(typeName);
        if (wti == null) {
            return addType(typeName);
        }
        
        return wti;
    }
    
    /* (non-Javadoc)
     * @see org.glassfish.hk2.configuration.hub.api.WriteableBeanDatabase#commit()
     */
    @Override
    public void commit() {
        Object defaultCommit;
        synchronized (this) {
            defaultCommit = commitMessage;
        }
        
        commit(defaultCommit);
    }

    /* (non-Javadoc)
     * @see org.glassfish.hk2.configuration.hub.api.WriteableBeanDatabase#commit()
     */
    @Override
    public void commit(Object commitMessage) {
        synchronized (this) {
            checkState();
        
            committed = true;
        }
        
        // Outside of lock
        hub.setCurrentDatabase(this, commitMessage, changes);
        
        for (WriteableTypeImpl removedType : removedTypes) {
            removedType.getHelper().dispose();
        }
        
        removedTypes.clear();
    }
    
    /* package */ long getBaseRevision() {
        return baseRevision;
    }
    
    /* package */ synchronized void addChange(Change change) {
        changes.add(change);
    }

    /* (non-Javadoc)
     * @see org.glassfish.hk2.configuration.hub.api.BeanDatabase#dumpDatabase()
     */
    @Override
    public void dumpDatabase() {
        dumpDatabase(System.err);
    }

    /* (non-Javadoc)
     * @see org.glassfish.hk2.configuration.hub.api.BeanDatabase#dumpDatabase(java.io.PrintStream)
     */
    @Override
    public synchronized void dumpDatabase(PrintStream output) {
        Utilities.dumpDatabase(this, output);        
    }
    
    /* (non-Javadoc)
     * @see org.glassfish.hk2.configuration.hub.api.BeanDatabase#dumpDatabaseAsString()
     */
    @Override
    public String dumpDatabaseAsString() {
        return Utilities.dumpDatabaseAsString(this);
    }
    
    /* (non-Javadoc)
     * @see org.glassfish.hk2.configuration.hub.api.WriteableBeanDatabase#getTwoPhaseResource()
     */
    @Override
    public TwoPhaseResource getTwoPhaseResource() {
        return resource;
    }
    
    /* (non-Javadoc)
     * @see org.glassfish.hk2.configuration.hub.api.WriteableBeanDatabase#getCommitMessage()
     */
    @Override
    public synchronized Object getCommitMessage() {
        return commitMessage;
    }

    /* (non-Javadoc)
     * @see org.glassfish.hk2.configuration.hub.api.WriteableBeanDatabase#setCommitMessage(java.lang.Object)
     */
    @Override
    public synchronized void setCommitMessage(Object commitMessage) {
        this.commitMessage = commitMessage;
    }

    

    private class TwoPhaseResourceImpl implements TwoPhaseResource {
        private LinkedList completedListeners;

        /* (non-Javadoc)
         * @see org.glassfish.hk2.api.TwoPhaseResource#prepareDynamicConfiguration(org.glassfish.hk2.api.TwoPhaseTransactionData)
         */
        @Override
        public void prepareDynamicConfiguration(
                TwoPhaseTransactionData dynamicConfiguration)
                throws MultiException {
            Object defaultCommit;
            synchronized (WriteableBeanDatabaseImpl.this) {
                checkState();
                
                committed = true;
                
                defaultCommit = commitMessage;
            }
            
            // Outside of lock
            completedListeners = hub.prepareCurrentDatabase(WriteableBeanDatabaseImpl.this, defaultCommit, changes);
        }

        /* (non-Javadoc)
         * @see org.glassfish.hk2.api.TwoPhaseResource#activateDynamicConfiguration(org.glassfish.hk2.api.TwoPhaseTransactionData)
         */
        @Override
        public void activateDynamicConfiguration(
                TwoPhaseTransactionData dynamicConfiguration) {
            LinkedList completedListeners = this.completedListeners;
            this.completedListeners = null;
            
            Object defaultCommit;
            synchronized (WriteableBeanDatabaseImpl.this) {
                defaultCommit = commitMessage;
            }
            
            hub.activateCurrentDatabase(WriteableBeanDatabaseImpl.this, defaultCommit, changes, completedListeners);
            
            for (WriteableTypeImpl removedType : removedTypes) {
                removedType.getHelper().dispose();
            }
            
            removedTypes.clear();
            
        }

        /* (non-Javadoc)
         * @see org.glassfish.hk2.api.TwoPhaseResource#rollbackDynamicConfiguration(org.glassfish.hk2.api.TwoPhaseTransactionData)
         */
        @Override
        public void rollbackDynamicConfiguration(
                TwoPhaseTransactionData dynamicConfiguration) {
            LinkedList completedListeners = this.completedListeners;
            this.completedListeners = null;
            
            Object defaultCommit;
            synchronized (WriteableBeanDatabaseImpl.this) {
                defaultCommit = commitMessage;
            }
            
            hub.rollbackCurrentDatabase(WriteableBeanDatabaseImpl.this, defaultCommit, changes, completedListeners);
            
            for (WriteableTypeImpl removedType : removedTypes) {
                removedType.getHelper().dispose();
            }
            
            removedTypes.clear();
        }
        
    }
    
    private String getChanges() {
        int lcv = 1;
        StringBuffer sb = new StringBuffer("\n");
        for (Change change : changes) {
            sb.append(lcv + ". " + change + "\n");
            lcv++;
        }
        
        return sb.toString();
    }
    
    @Override
    public String toString() {
        return "WriteableBeanDatabaseImpl(" + baseRevision + ",changes=" + getChanges() + "," + System.identityHashCode(this) + ")";
    }

    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy