001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019: package org.netbeans.modules.xslt.tmap.model.impl;
020:
021: import java.io.IOException;
022: import java.util.concurrent.Callable;
023: import java.util.concurrent.atomic.AtomicReference;
024: import java.util.concurrent.locks.Lock;
025: import java.util.concurrent.locks.ReentrantReadWriteLock;
026: import org.netbeans.modules.xml.xam.ComponentUpdater;
027: import org.netbeans.modules.xml.xam.ModelSource;
028: import org.netbeans.modules.xml.xam.dom.AbstractDocumentModel;
029: import org.netbeans.modules.xslt.tmap.model.api.TMapComponent;
030: import org.netbeans.modules.xslt.tmap.model.api.TMapComponentFactory;
031: import org.netbeans.modules.xslt.tmap.model.api.TMapModel;
032: import org.netbeans.modules.xslt.tmap.model.api.TransformMap;
033: import org.openide.ErrorManager;
034: import org.openide.util.Exceptions;
035: import org.w3c.dom.Element;
036:
037: /**
038: *
039: * @author Vitaly Bychkov
040: * @version 1.0
041: */
042: public class TMapModelImpl extends AbstractDocumentModel<TMapComponent>
043: implements TMapModel {
044: private TMapComponentFactory myFactory;
045: private TransformMap myRoot;
046: private AtomicReference<SyncUpdateVisitor> myUpdateVisitor = new AtomicReference<SyncUpdateVisitor>();
047:
048: private final ReentrantReadWriteLock myLock = new ReentrantReadWriteLock();
049:
050: private final Lock readLock = myLock.readLock();
051:
052: private final Lock writeLock = myLock.writeLock();
053:
054: public TMapModelImpl(ModelSource source) {
055: super (source);
056: myFactory = new TMapComponentFactoryImpl(this );
057: }
058:
059: public TMapComponent createRootComponent(Element root) {
060: TransformMap transformMap = (TransformMap) myFactory.create(
061: root, null);
062: if (transformMap != null) {
063: myRoot = transformMap;
064: } else {
065: return null;
066: }
067: return getTransformMap();
068: }
069:
070: protected ComponentUpdater<TMapComponent> getComponentUpdater() {
071: SyncUpdateVisitor updater = myUpdateVisitor.get();
072: if (updater == null) {
073: myUpdateVisitor
074: .compareAndSet(null, new SyncUpdateVisitor());
075: updater = myUpdateVisitor.get();
076: }
077: return updater;
078: }
079:
080: public TransformMap getTransformMap() {
081: return (TransformMap) getRootComponent();
082: }
083:
084: public TMapComponent getRootComponent() {
085: return myRoot;
086: }
087:
088: public TMapComponent createComponent(TMapComponent parent,
089: Element element) {
090: return getFactory().create(element, parent);
091: }
092:
093: public TMapComponentFactory getFactory() {
094: return myFactory;
095: }
096:
097: public <V> V invoke(Callable<V> action) throws Exception {
098: // boolean isInTransaction = isIntransaction();
099: V result = null;
100:
101: try {
102: // if (!isInTransaction) {
103: startTransaction();
104: // }
105:
106: result = action.call();
107: } finally {
108: // if (!isInTransaction) {
109: endTransaction();
110: // }
111: }
112: return result;
113: }
114: }
|