Package org.geotools.data

Examples of org.geotools.data.FeatureLock


      throws InterruptedException,
      IOException {
    final LockingManagement memoryLockManager = new MemoryLockManager(
        "default");
    final Transaction t1 = Transaction.AUTO_COMMIT;
    FeatureLock lock = new FeatureLock(
        "auth1",
        1 /* minute */);
    memoryLockManager.lockFeatureID(
        "sometime",
        "f1",
 
View Full Code Here


     *             if a lock failed and the lock specified all locks, or if an
     *             another error occurred processing the lock operation
     */
    public LockFeatureResponse lockFeature(LockFeatureRequest request)
        throws WFSException {
        FeatureLock fLock = null;

        try {
            // check we are dealing with a well formed request, there is at
            // least on lock request?
            List<Lock> locks = request.getLocks();

            if ((locks == null) || locks.isEmpty()) {
                String msg = "A LockFeature request must contain at least one LOCK element";
                throw new WFSException(request, msg);
            }

            LOGGER.info("locks size is " + locks.size());

            // create a new lock (token used to manage locks across datastores)
            fLock = newFeatureLock(request);

            // prepare the response object
            LockFeatureResponse response = request.createResponse();
            response.setLockId(fLock.getAuthorization());
           
            // go thru each lock request, and try to perform locks on a feature
            // by feature basis
            // in order to allow for both "all" and "some" lock behaviour
            // TODO: if the lock is the default this default, lock the whole
            // query directly, should be a lot faster
            for (int i = 0, n = locks.size(); i < n; i++) {
                Lock lock = locks.get(i);
                LOGGER.info("curLock is " + lock);

                QName typeName = lock.getTypeName();

                // get out the filter, and default to no filtering if none was
                // provided
                Filter filter = lock.getFilter();

                if (filter == null) {
                    filter = Filter.INCLUDE;
                }

                FeatureTypeInfo meta;
                FeatureSource<? extends FeatureType, ? extends Feature> source;
                FeatureCollection<? extends FeatureType, ? extends Feature> features;

                try {
                    meta = catalog.getFeatureTypeByName(typeName.getNamespaceURI(), typeName.getLocalPart());

                    if (meta == null) {
                        throw new WFSException(request, "Unknown feature type " + typeName.getPrefix() + ":"
                            + typeName.getLocalPart());
                    }

                    source = meta.getFeatureSource(null,null);
                   
                    // make sure all geometric elements in the filter have a crs, and that the filter
                    // is reprojected to store's native crs as well
                    CoordinateReferenceSystem declaredCRS = WFSReprojectionUtil.getDeclaredCrs(
                            source.getSchema(), request.getVersion());
                    filter = WFSReprojectionUtil.normalizeFilterCRS(filter, source.getSchema(), declaredCRS);
                   
                    // now gather the features
                    features = source.getFeatures(filter);

                    if (source instanceof FeatureLocking) {
                        ((FeatureLocking) source).setFeatureLock(fLock);
                    }
                } catch (IOException e) {
                    throw new WFSException(request, e);
                }

                FeatureIterator reader = null;
                int numberLocked = -1;

                try {
                    for (reader = features.features(); reader.hasNext();) {
                        SimpleFeature feature = (SimpleFeature) reader.next();

                        FeatureId fid = fid(feature.getID());
                        Id fidFilter = fidFilter(fid);

                        if (!(source instanceof FeatureLocking)) {
                            LOGGER.fine("Lock " + fid + " not supported by data store (authID:"
                                + fLock.getAuthorization() + ")");

                            response.addNotLockedFeature(fid);
                            // lockFailedFids.add(fid);
                        } else {
                            // DEFQuery is just some indirection, should be in
                            // the locking interface.
                            // int numberLocked =
                            // ((DEFQueryFeatureLocking)source).lockFeature(feature);
                            // HACK: Query.NO_NAMES isn't working in postgis
                            // right now,
                            // so we'll just use all.
                            Query query = new Query(meta.getName(), (Filter) fidFilter,
                                    Query.DEFAULT_MAX, Query.ALL_NAMES, lock.getHandle());

                            numberLocked = ((FeatureLocking) source).lockFeatures(query);

                            if (numberLocked == 1) {
                                LOGGER.fine("Lock " + fid + " (authID:" + fLock.getAuthorization()
                                    + ")");
                                response.addLockedFeature( fid);

                                // lockedFids.add(fid);
                            } else if (numberLocked == 0) {
                                LOGGER.fine("Lock " + fid + " conflict (authID:"
                                    + fLock.getAuthorization() + ")");
                                response.addNotLockedFeature(fid);

                                // lockFailedFids.add(fid);
                            } else {
                                LOGGER.warning("Lock " + numberLocked + " " + fid + " (authID:"
                                    + fLock.getAuthorization() + ") duplicated FeatureID!");
                                response.addLockedFeature(fid);

                                // lockedFids.add(fid);
                            }
                        }
                    }
                } catch (IOException ioe) {
                    throw new WFSException(request, ioe);
                } finally {
                    if (reader != null) {
                        reader.close();
                    }
                }

                // refresh lock times, so they all start the same instant and we
                // are nearer
                // to the spec when it says the expiry should start when the
                // lock
                // feature response has been totally written
                if (numberLocked > 0) {
                    Transaction t = new DefaultTransaction();

                    try {
                        try {
                            t.addAuthorization(fLock.getAuthorization());
                            DataStore dataStore = (DataStore) source.getDataStore();
                            dataStore.getLockingManager().refresh(fLock.getAuthorization(), t);
                        } finally {
                            t.commit();
                        }
                    } catch (IOException e) {
                        throw new WFSException(request, e);
                    } finally {
                        try {
                            t.close();
                        } catch(IOException e) {
                            throw new WFSException(request, e);
                        }
                    }
                }
            }

            // should we releas all? if not set default to true
           
            boolean lockAll = !request.isLockActionSome();

            List notLocked = response.getNotLockedFeatures();
            if (lockAll && (notLocked != null && !notLocked.isEmpty())) {
                // I think we need to release and fail when lockAll fails
                //
                // abort will release the locks
                throw new WFSException(request, "Could not aquire locks for:" + notLocked);
            }

            return response;
        } catch (WFSException e) {
            // release locks when something fails
            if (fLock != null) {
                try {
                    release(fLock.getAuthorization());
                } catch (WFSException e1) {
                    // log it
                    LOGGER.log(Level.SEVERE, "Error occured releasing locks", e1);
                }
            }
View Full Code Here

    @Test
    public void testLockUnlockFilter() throws Exception {
        SimpleFeatureLocking fl;
        fl = (SimpleFeatureLocking) rts.getFeatureSource(RENAMED);
        final FeatureLock lock = FeatureLockFactory.generate(10 * 60 * 1000);
        Transaction t = new DefaultTransaction();
        t.addAuthorization(lock.getAuthorization());
        fl.setTransaction(t);
        fl.setFeatureLock(lock);

        SimpleFeatureLocking fl2;
        fl2 = (SimpleFeatureLocking) rts.getFeatureSource(RENAMED);
View Full Code Here

   
    @Test
    public void testLockUnlockQuery() throws Exception {
        SimpleFeatureLocking fl;
        fl = (SimpleFeatureLocking) rts.getFeatureSource(RENAMED);
        final FeatureLock lock = FeatureLockFactory.generate(10 * 60 * 1000);
        Transaction t = new DefaultTransaction();
        t.addAuthorization(lock.getAuthorization());
        fl.setTransaction(t);
        fl.setFeatureLock(lock);

        SimpleFeatureLocking fl2;
        fl2 = (SimpleFeatureLocking) rts.getFeatureSource(RENAMED);
View Full Code Here

    @Test
    public void testLockUnlockFilter() throws Exception {
        SimpleFeatureLocking fl;
        fl = (SimpleFeatureLocking) rts.getFeatureSource(RENAMED);
        final FeatureLock lock = FeatureLockFactory.generate(10 * 60 * 1000);
        Transaction t = new DefaultTransaction();
        t.addAuthorization(lock.getAuthorization());
        fl.setTransaction(t);
        fl.setFeatureLock(lock);

        SimpleFeatureLocking fl2;
        fl2 = (SimpleFeatureLocking) rts.getFeatureSource(RENAMED);
View Full Code Here

   
    @Test
    public void testLockUnlockQuery() throws Exception {
        SimpleFeatureLocking fl;
        fl = (SimpleFeatureLocking) rts.getFeatureSource(RENAMED);
        final FeatureLock lock = FeatureLockFactory.generate(10 * 60 * 1000);
        Transaction t = new DefaultTransaction();
        t.addAuthorization(lock.getAuthorization());
        fl.setTransaction(t);
        fl.setFeatureLock(lock);

        SimpleFeatureLocking fl2;
        fl2 = (SimpleFeatureLocking) rts.getFeatureSource(RENAMED);
View Full Code Here

            transformer.addSchemaLocation(uri, (String) ftNamespaces.get(uri));
        }

        transformer.setGmlPrefixing(request.getWFS().getCiteConformanceHacks());

        FeatureLock featureLock = results.getFeatureLock();

        if (featureLock != null) {
            transformer.setLockId(featureLock.getAuthorization());
        }

        transformer.setSrsName(request.getWFS().getSrsPrefix() + meta.getSRS());
    }
View Full Code Here

        }

        LockRequest.Lock curLock = (LockRequest.Lock) locks.get(0);
        boolean lockAll = request.getLockAll();

        FeatureLock fLock = request.toFeatureLock();
        Set lockedFids = new HashSet();
        Set lockFailedFids = new HashSet();
        GeoServer config = request.getGeoServer();
        Data catalog = request.getWFS().getData();
        FilterFactory filterFactory = FilterFactory.createFilterFactory();
        LOGGER.info("locks size is " + locks.size());

        if (locks.size() == 0) {
            throw new WfsException("Lock Request must contain at least one "
                + " Lock element, your request is " + request);
        }

        for (int i = 0, n = locks.size(); i < n; i++) {
            curLock = (LockRequest.Lock) locks.get(i);
            LOGGER.info("curLock is " + curLock);

            String curTypeName = curLock.getFeatureType();
            Filter curFilter = curLock.getFilter();

            FeatureTypeInfo meta = catalog.getFeatureTypeInfo(curTypeName);
            NameSpaceInfo namespace = meta.getDataStoreInfo().getNameSpace();
            FeatureSource source = meta.getFeatureSource();
            FeatureResults features = source.getFeatures(curFilter);

            if( source instanceof FeatureLocking){
                ((FeatureLocking)source).setFeatureLock(fLock);
            }
            FeatureReader reader = null;

            try {
                for (reader = features.reader(); reader.hasNext();) {
                    Feature feature = reader.next();
                    String fid = feature.getID();
                    if( !(source instanceof FeatureLocking)){
                        LOGGER.fine("Lock " + fid +
                                " not supported by data store (authID:"
                                + fLock.getAuthorization() + ")");
                        lockFailedFids.add(fid);
                    }
                    else {
                        Filter fidFilter = filterFactory.createFidFilter(fid);

                        //DEFQuery is just some indirection, should be in the locking interface.
                        //int numberLocked = ((DEFQueryFeatureLocking)source).lockFeature(feature);
                        //HACK: Query.NO_NAMES isn't working in postgis right now,
                        //so we'll just use all.
                        Query query = new DefaultQuery(
                                meta.getTypeName(), fidFilter,
                                Query.DEFAULT_MAX, Query.ALL_NAMES,
                                curLock.getHandle());
                        int numberLocked = ((FeatureLocking)source).lockFeatures( query );

                        if (numberLocked == 1) {
                            LOGGER.fine("Lock " + fid + " (authID:"
                                + fLock.getAuthorization() + ")");
                            lockedFids.add(fid);
                        } else if (numberLocked == 0) {
                            LOGGER.fine("Lock " + fid + " conflict (authID:"
                                + fLock.getAuthorization() + ")");
                            lockFailedFids.add(fid);
                        } else {
                            LOGGER.warning("Lock " + numberLocked + " " + fid
                                + " (authID:" + fLock.getAuthorization()
                                + ") duplicated FeatureID!");
                            lockedFids.add(fid);
                        }
                    }
                }
            } catch (IllegalAttributeException e) {
                // TODO: JG - I really dont like this
                // reader says it will throw this if the attribtues do not match
                // the FeatureTypeInfo
                // I figure if this is thrown we are poorly configured or
                // the DataStoreInfo needs some quality control
                //
                // should rollback the lock as well :-(
                throw new WfsException("Lock request " + curFilter
                    + " did not match " + curTypeName);
            } finally {
                if (reader != null) {
                    reader.close();
                }
            }
        }

        if (lockAll && !lockFailedFids.isEmpty()) {
            // I think we need to release and fail when lockAll fails
            //
            // abort will release the locks
            throw new WfsException("Could not aquire locks for:"
                + lockFailedFids);
        }

        if (getXml) {
            return generateXml(fLock.getAuthorization(), lockAll,
                lockedFids, lockFailedFids, request);
        } else {
            return fLock.getAuthorization();
        }
    }
View Full Code Here

    }

    public void testLockUnlockFilter() throws Exception {
        SimpleFeatureLocking fl;
        fl = (SimpleFeatureLocking) rts.getFeatureSource(RENAMED);
        final FeatureLock lock = FeatureLockFactory.generate(10 * 60 * 1000);
        Transaction t = new DefaultTransaction();
        t.addAuthorization(lock.getAuthorization());
        fl.setTransaction(t);
        fl.setFeatureLock(lock);

        SimpleFeatureLocking fl2;
        fl2 = (SimpleFeatureLocking) rts.getFeatureSource(RENAMED);
View Full Code Here

    }
   
    public void testLockUnlockQuery() throws Exception {
        SimpleFeatureLocking fl;
        fl = (SimpleFeatureLocking) rts.getFeatureSource(RENAMED);
        final FeatureLock lock = FeatureLockFactory.generate(10 * 60 * 1000);
        Transaction t = new DefaultTransaction();
        t.addAuthorization(lock.getAuthorization());
        fl.setTransaction(t);
        fl.setFeatureLock(lock);

        SimpleFeatureLocking fl2;
        fl2 = (SimpleFeatureLocking) rts.getFeatureSource(RENAMED);
View Full Code Here

TOP

Related Classes of org.geotools.data.FeatureLock

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.