List of usage examples for java.util.concurrent.locks Lock unlock
void unlock();
From source file:org.geotools.gce.imagemosaic.catalog.GTDataStoreGranuleCatalog.java
public void createType(String namespace, String typeName, String typeSpec) throws IOException, SchemaException { Utilities.ensureNonNull("typeName", typeName); Utilities.ensureNonNull("typeSpec", typeSpec); final Lock lock = rwLock.writeLock(); String type = null;//from w w w .ja va2 s. co m try { lock.lock(); checkStore(); final SimpleFeatureType featureType = DataUtilities.createType(namespace, typeName, typeSpec); tileIndexStore.createSchema(featureType); type = featureType.getTypeName(); if (typeName != null) { addTypeName(typeName, true); } extractBasicProperties(type); } finally { lock.unlock(); } }
From source file:org.geotools.gce.imagemosaic.catalog.GTDataStoreGranuleCatalog.java
public void createType(String identification, String typeSpec) throws SchemaException, IOException { Utilities.ensureNonNull("typeSpec", typeSpec); Utilities.ensureNonNull("identification", identification); final Lock lock = rwLock.writeLock(); String typeName = null;/* w w w . j av a 2 s . c o m*/ try { lock.lock(); checkStore(); final SimpleFeatureType featureType = DataUtilities.createType(identification, typeSpec); tileIndexStore.createSchema(featureType); typeName = featureType.getTypeName(); if (typeName != null) { addTypeName(typeName, true); } extractBasicProperties(typeName); } finally { lock.unlock(); } }
From source file:org.geotools.gce.imagemosaic.catalog.GTDataStoreGranuleCatalog.java
public void computeAggregateFunction(Query query, FeatureCalc function) throws IOException { query = mergeHints(query);/* w w w . j ava 2s . co m*/ final Lock lock = rwLock.readLock(); try { lock.lock(); checkStore(); SimpleFeatureSource fs = tileIndexStore.getFeatureSource(query.getTypeName()); if (fs instanceof ContentFeatureSource) ((ContentFeatureSource) fs).accepts(query, function, null); else { final SimpleFeatureCollection collection = fs.getFeatures(query); collection.accepts(function, null); } } finally { lock.unlock(); } }
From source file:com.funambol.pushlistener.service.taskexecutor.ScheduledTaskExecutor.java
/** * Schedules a new task// w w w . j a v a 2 s. co m * * @param task the <code>ScheduledTaskWrapper</code> to schedule */ public void scheduleTask(ScheduledTaskWrapper task) { if (task == null) { if (log.isTraceEnabled()) { log.trace("Trying to schedule a null task. Request rejected"); } return; } // // Locking the lock for this task so no other thread can handle it avoiding // conflict (what happens if a thread is removing it an another threead is // updating it ?) // Lock handlingTaskLock = getHandlingTaskLock(task.getId()); handlingTaskLock.lock(); try { scheduleTask(task, 0); } finally { handlingTaskLock.unlock(); } }
From source file:org.pepstock.jem.gwt.server.services.StatisticsManager.java
/** * Returns the collection of all active samples in JEM. * // ww w . j av a 2 s .com * @return collection of samples * @throws ServiceMessageException if any exception occurs */ public Collection<LightSample> getSamples() throws ServiceMessageException { // Remember that it uses QUEUES CURRENT permission to check if // it can get statistics // checks if the user is authorized // if not, this method throws an exception try { checkAuthorization(new StringPermission(Permissions.ADMINISTRATION_CLUSTER_FOLDER)); } catch (Exception e) { LogAppl.getInstance().ignore(e.getMessage(), e); try { checkAuthorization(new StringPermission(Permissions.ADMINISTRATION_NODES_FOLDER)); } catch (Exception e1) { LogAppl.getInstance().ignore(e1.getMessage(), e1); checkAuthorization(new StringPermission(Permissions.ADMINISTRATION_QUEUES_FOLDER)); } } IMap<String, LightSample> samples = getInstance().getMap(Queues.STATS_MAP); Lock lock = getInstance().getLock(Queues.STATS_MAP_LOCK); boolean isLock = false; List<LightSample> list = null; try { // locks all map to have a consistent collection // only for 10 seconds otherwise // throws an exception isLock = lock.tryLock(10, TimeUnit.SECONDS); if (isLock) { // gets data... list = new ArrayList<LightSample>(samples.values()); // ... and sorts them Collections.sort(list, sampleComparator); } else { // timeout exception throw new ServiceMessageException(UserInterfaceMessage.JEMG022E, Queues.STATS_MAP_LOCK); } } catch (InterruptedException e) { // timeout exception throw new ServiceMessageException(UserInterfaceMessage.JEMG022E, e, Queues.STATS_MAP_LOCK); } finally { // unlocks always the map if (isLock) { lock.unlock(); } } return list; }
From source file:com.haulmont.cuba.web.security.idp.BaseIdpSessionFilter.java
@Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // send static files without authentication HttpServletRequest httpRequest = (HttpServletRequest) request; if (StringUtils.startsWith(httpRequest.getRequestURI(), httpRequest.getContextPath() + "/VAADIN/")) { chain.doFilter(request, response); return;/*from w w w . j a v a 2 s . c om*/ } HttpServletResponse httpResponse = (HttpServletResponse) response; String idpBaseURL = webIdpConfig.getIdpBaseURL(); if (Strings.isNullOrEmpty(idpBaseURL)) { log.error("Application property cuba.web.idp.url is not set"); httpResponse.setStatus(500); return; } if (!idpBaseURL.endsWith("/")) { idpBaseURL += "/"; } String requestUrl = httpRequest.getRequestURL().toString(); if (StringUtils.startsWith(requestUrl, idpBaseURL)) { chain.doFilter(httpRequest, response); return; } HttpSession session = httpRequest.getSession(true); Lock sessionLock = (Lock) session.getAttribute(IDP_SESSION_LOCK_ATTRIBUTE); if (sessionLock == null) { sessionCheckLock.lock(); try { sessionLock = (Lock) session.getAttribute(IDP_SESSION_LOCK_ATTRIBUTE); if (sessionLock == null) { sessionLock = new ReentrantLock(); session.setAttribute(IDP_SESSION_LOCK_ATTRIBUTE, sessionLock); } } finally { sessionCheckLock.unlock(); } } IdpSession boundIdpSession; sessionLock.lock(); try { session.getAttribute(IDP_SESSION_LOCK_ATTRIBUTE); } catch (IllegalStateException e) { // Someone might have invalidated the session between fetching the lock and acquiring it. sessionLock.unlock(); log.debug("Invalidated session {}", session.getId()); httpResponse.sendRedirect(httpRequest.getRequestURL().toString()); return; } try { if ("GET".equals(httpRequest.getMethod()) && httpRequest.getParameter(IDP_TICKET_REQUEST_PARAM) != null) { String idpTicket = httpRequest.getParameter(IDP_TICKET_REQUEST_PARAM); IdpSession idpSession; try { idpSession = getIdpSession(idpTicket); } catch (IdpActivationException e) { log.error("Unable to obtain IDP session by ticket", e); httpResponse.setStatus(500); return; } if (idpSession == null) { log.warn("Used old IDP ticket {}, send redirect", idpTicket); // used old ticket, send redirect httpResponse.sendRedirect(getIdpRedirectUrl()); return; } session.invalidate(); session = httpRequest.getSession(true); session.setAttribute(IDP_SESSION_LOCK_ATTRIBUTE, sessionLock); session.setAttribute(IDP_SESSION_ATTRIBUTE, idpSession); log.debug("IDP session {} obtained, redirect to application", idpSession); // redirect to application without parameters httpResponse.sendRedirect(httpRequest.getRequestURL().toString()); return; } if (session.getAttribute(IDP_SESSION_ATTRIBUTE) == null) { if ("GET".equals(httpRequest.getMethod()) && !StringUtils.startsWith(httpRequest.getRequestURI(), httpRequest.getContextPath() + "/PUSH")) { httpResponse.sendRedirect(getIdpRedirectUrl()); } return; } boundIdpSession = (IdpSession) session.getAttribute(IDP_SESSION_ATTRIBUTE); } finally { sessionLock.unlock(); } HttpServletRequest authenticatedRequest = new IdpServletRequestWrapper(httpRequest, new IdpSessionPrincipalImpl(boundIdpSession)); chain.doFilter(authenticatedRequest, response); }
From source file:com.funambol.pushlistener.service.taskexecutor.ScheduledTaskExecutor.java
/** * Executes the given task scheduling its execution to run after a given delay. * <p>Note that the task will be executed as soon as a thread * is available. Moreover, if the same task (or another equal task) is already * running, a new execution is planned with a delay of task.getPeriod()). * If the same task (or another equal task) is already waiting for its execution * this method doesn't have effect./*w w w .j av a 2 s . co m*/ * @return true if the task execution has been planned to be performed as soon as * possible, false otherwise * (maybe the same task or another equal task is waiting for its execution or * is already running) * @param newTask the task to execute * @param delay the execution delay */ public boolean executeTaskWrapper(TaskWrapper newTask, long delay) { if (newTask == null) { throw new IllegalArgumentException("Task must be not null"); } Lock handlingTaskLock = getHandlingTaskLock(newTask); handlingTaskLock.lock(); try { return submitTaskWrapper(newTask, delay); } finally { handlingTaskLock.unlock(); } }
From source file:com.cloudera.oryx.als.serving.ServerRecommender.java
@Override public float[] estimatePreferences(String userID, String... itemIDs) throws NotReadyException { Generation generation = getCurrentGeneration(); LongObjectMap<float[]> X = generation.getX(); float[] userFeatures; Lock xLock = generation.getXLock().readLock(); xLock.lock();/*ww w . j a v a2s. co m*/ try { userFeatures = X.get(StringLongMapping.toLong(userID)); } finally { xLock.unlock(); } if (userFeatures == null) { return new float[itemIDs.length]; // All 0.0f } LongObjectMap<float[]> Y = generation.getY(); Lock yLock = generation.getYLock().readLock(); yLock.lock(); try { float[] result = new float[itemIDs.length]; for (int i = 0; i < itemIDs.length; i++) { String itemID = itemIDs[i]; float[] itemFeatures = Y.get(StringLongMapping.toLong(itemID)); if (itemFeatures != null) { float value = (float) SimpleVectorMath.dot(itemFeatures, userFeatures); Preconditions.checkState(Floats.isFinite(value), "Bad estimate"); result[i] = value; } // else leave value at 0.0f } return result; } finally { yLock.unlock(); } }
From source file:be.fgov.kszbcss.rhq.websphere.connector.security.TrustStoreManager.java
private KeyStore loadTrustStore() throws GeneralSecurityException, IOException { Lock lock = truststoreLock.readLock(); lock.lock();//from www. ja v a2 s . c o m try { KeyStore truststore = KeyStore.getInstance("JKS"); if (truststoreFile.exists()) { if (log.isDebugEnabled()) { log.debug("Loading existing trust store from " + truststoreFile); } InputStream in = new FileInputStream(truststoreFile); try { truststore.load(in, new char[0]); } finally { in.close(); } if (log.isDebugEnabled()) { log.debug("Trust store has " + truststore.size() + " existing entries"); } } else { if (log.isDebugEnabled()) { log.debug("Trust store " + truststoreFile + " doesn't exist yet; a new one will be created if necessary"); } truststore.load(null); } return truststore; } finally { lock.unlock(); } }
From source file:org.pepstock.jem.gwt.server.services.NodesManager.java
private List<NodeInfoBean> getNodesButUnknown(AbstractPredicate predicate) throws ServiceMessageException { IMap<String, NodeInfo> nodes = getInstance().getMap(Queues.NODES_MAP); List<NodeInfoBean> list = new ArrayList<NodeInfoBean>(); Collection<NodeInfo> allNodes = null; boolean isLock = false; Lock lock = getInstance().getLock(Queues.NODES_MAP_LOCK); try {/*w ww . ja v a 2s . c o m*/ isLock = lock.tryLock(10, TimeUnit.SECONDS); if (isLock) { // gets all nodes by predicate allNodes = nodes.values(predicate); } else { throw new ServiceMessageException(UserInterfaceMessage.JEMG022E, Queues.NODES_MAP); } } catch (InterruptedException e) { throw new ServiceMessageException(UserInterfaceMessage.JEMG022E, e, Queues.NODES_MAP); } finally { // unlocks always the map if (isLock) { lock.unlock(); } } if (allNodes != null) { // gets the nodes and returns them // removing the nodes in UNKNOW // to avoid misunderstanding on UI for (NodeInfo node : allNodes) { if (!node.getStatus().equals(Status.UNKNOWN)) { list.add(node.getNodeInfoBean()); } } } return list; }