List of usage examples for java.lang Long decode
public static Long decode(String nm) throws NumberFormatException
From source file:org.ohmage.query.impl.SurveyUploadQuery.java
/** * Checks again that the current leaf directory is full. If it is not, then * it will just back out under the impression someone else made the change. * If it is, it will go up and down the directory tree structure to find a * new leaf node in which to store new files. * // w w w . jav a 2 s.co m * @param numFilesPerDirectory The maximum allowed number of files in a * leaf directory and the maximum allowed * number of directories in the branches. */ private synchronized void getNewDirectory(int numFilesPerDirectory) throws DataAccessException { try { // Make sure that this hasn't changed because another thread may // have preempted us and already changed the current leaf // directory. File[] files = imageLeafDirectory.listFiles(); if (files.length < numFilesPerDirectory) { return; } // Get the root directory from the preference cache based on the // key. String rootFile; try { rootFile = PreferenceCache.instance().lookup(PreferenceCache.KEY_IMAGE_DIRECTORY); } catch (CacheMissException e) { throw new DataAccessException( "Preference cache doesn't know about 'known' key: " + PreferenceCache.KEY_IMAGE_DIRECTORY, e); } File rootDirectory = new File(rootFile); if (!rootDirectory.exists()) { throw new DataAccessException( "The root file doesn't exist suggesting an incomplete installation: " + rootFile); } else if (!rootDirectory.isDirectory()) { throw new DataAccessException("The root file isn't a directory."); } String absoluteRootDirectory = rootDirectory.getAbsolutePath(); // A filter when listing a set of directories for a file. DirectoryFilter directoryFilter = new DirectoryFilter(); // A local File to use while we are searching to not confuse other // threads. File newDirectory = imageLeafDirectory; // A flag to indicate when we are done looking for a directory. boolean lookingForDirectory = true; // The number of times we stepped up in the hierarchy. int depth = 0; // While we are still looking for a suitable directory, while (lookingForDirectory) { // Get the current directory's name which should be a Long // value. long currDirectoryName; try { String dirName = newDirectory.getName(); while (dirName.startsWith("0")) { dirName = dirName.substring(1); } if ("".equals(dirName)) { currDirectoryName = 0; } else { currDirectoryName = Long.decode(dirName); } } catch (NumberFormatException e) { if (newDirectory.getAbsolutePath().equals(absoluteRootDirectory)) { throw new DataAccessException("Document structure full!", e); } else { throw new DataAccessException("Potential breach of document structure.", e); } } // Move the pointer up a directory. newDirectory = new File(newDirectory.getParent()); // Get the list of files in the parent. File[] parentDirectoryFiles = newDirectory.listFiles(directoryFilter); // If this directory has room for a new subdirectory, if (parentDirectoryFiles.length < numFilesPerDirectory) { // Increment the name for the next subfolder. currDirectoryName++; // Create the new subfolder. newDirectory = new File(newDirectory.getAbsolutePath() + "/" + directoryNameBuilder(currDirectoryName, numFilesPerDirectory)); newDirectory.mkdir(); // Continue drilling down to reach an appropriate leaf // node. while (depth > 0) { newDirectory = new File(newDirectory.getAbsolutePath() + "/" + directoryNameBuilder(0, numFilesPerDirectory)); newDirectory.mkdir(); depth--; } lookingForDirectory = false; } // If the parent is full as well, increment the depth unless // we are already at the parent. If we are at the parent, then // we cannot go up any further and have exhausted the // directory structure. else { if (newDirectory.getAbsoluteFile().equals(absoluteRootDirectory)) { throw new DataAccessException("Document structure full!"); } else { depth++; } } } imageLeafDirectory = newDirectory; } catch (NumberFormatException e) { throw new DataAccessException("Could not decode a directory name as an integer.", e); } }
From source file:org.ohmage.query.impl.DocumentQueries.java
/** * Locks the creation lock and checks again that the current leaf * directory is full. If it is not, then it will just back out under the * impression someone else made the change. If it is, it will go up and * down the directory tree structure to find a new leaf node in which to * store new files./*from ww w .j av a 2 s . c om*/ * * @param numFilesPerDirectory The maximum allowed number of files in a * leaf directory and the maximum allowed * number of directories in the branches. */ private void getNewDirectory(int numFilesPerDirectory) throws DataAccessException { // Get the lock. DIRECTORY_CREATION_LOCK.lock(); try { // Make sure that this hasn't changed because another thread may // have preempted us and already changed the current leaf // directory. File[] files = currLeafDirectory.listFiles(); if (files.length < numFilesPerDirectory) { return; } // Get the root directory from the preference cache based on the // key. String rootFile; try { rootFile = PreferenceCache.instance().lookup(PreferenceCache.KEY_DOCUMENT_DIRECTORY); } catch (CacheMissException e) { throw new DataAccessException("Preference cache doesn't know about 'known' key: " + PreferenceCache.KEY_DOCUMENT_DIRECTORY, e); } File rootDirectory = new File(rootFile); if (!rootDirectory.exists()) { throw new DataAccessException( "The root file doesn't exist suggesting an incomplete installation: " + rootFile); } else if (!rootDirectory.isDirectory()) { throw new DataAccessException("The root file isn't a directory."); } String absoluteRootDirectory = rootDirectory.getAbsolutePath(); // A filter when listing a set of directories for a file. DirectoryFilter directoryFilter = new DirectoryFilter(); // A local File to use while we are searching to not confuse other // threads. File newDirectory = currLeafDirectory; // A flag to indicate when we are done looking for a directory. boolean lookingForDirectory = true; // The number of times we stepped up in the hierarchy. int depth = 0; // While we are still looking for a suitable directory, while (lookingForDirectory) { // Get the current directory's name which should be a Long // Valvalueue. long currDirectoryName; try { String dirName = newDirectory.getName(); while (dirName.startsWith("0")) { dirName = dirName.substring(1); } if ("".equals(dirName)) { currDirectoryName = 0; } else { currDirectoryName = Long.decode(dirName); } } catch (NumberFormatException e) { if (newDirectory.getAbsolutePath().equals(absoluteRootDirectory)) { throw new DataAccessException("Document structure full!", e); } else { throw new DataAccessException("Potential breach of document structure.", e); } } // Move the pointer up a directory. newDirectory = new File(newDirectory.getParent()); // Get the list of files in the parent. File[] parentDirectoryFiles = newDirectory.listFiles(directoryFilter); // If this directory has room for a new subdirectory, if (parentDirectoryFiles.length < numFilesPerDirectory) { // Increment the name for the next subfolder. currDirectoryName++; // Create the new subfolder. newDirectory = new File(newDirectory.getAbsolutePath() + "/" + directoryNameBuilder(currDirectoryName, numFilesPerDirectory)); newDirectory.mkdir(); // Continue drilling down to reach an appropriate leaf // node. while (depth > 0) { newDirectory = new File(newDirectory.getAbsolutePath() + "/" + directoryNameBuilder(0, numFilesPerDirectory)); newDirectory.mkdir(); depth--; } lookingForDirectory = false; } // If the parent is full as well, increment the depth unless // we are already at the parent. If we are at the parent, then // we cannot go up any further and have exhausted the // directory structure. else { if (newDirectory.getAbsoluteFile().equals(absoluteRootDirectory)) { throw new DataAccessException("Document structure full!"); } else { depth++; } } } currLeafDirectory = newDirectory; } catch (NumberFormatException e) { throw new DataAccessException("Could not decode a directory name as an integer.", e); } finally { DIRECTORY_CREATION_LOCK.unlock(); } }
From source file:org.apache.pulsar.broker.admin.impl.NamespacesBase.java
protected BundlesData validateBundlesData(BundlesData initialBundles) { SortedSet<String> partitions = new TreeSet<String>(); for (String partition : initialBundles.getBoundaries()) { Long partBoundary = Long.decode(partition); partitions.add(String.format("0x%08x", partBoundary)); }//ww w. j ava 2 s . c o m if (partitions.size() != initialBundles.getBoundaries().size()) { log.debug("Input bundles included repeated partition points. Ignored."); } try { NamespaceBundleFactory.validateFullRange(partitions); } catch (IllegalArgumentException iae) { throw new RestException(Status.BAD_REQUEST, "Input bundles do not cover the whole hash range. first:" + partitions.first() + ", last:" + partitions.last()); } List<String> bundles = Lists.newArrayList(); bundles.addAll(partitions); return new BundlesData(bundles); }
From source file:org.ohmage.domain.campaign.Campaign.java
/** * Process the property Nodes from the XML and creates a mapping of all of * the keys to label/value pairs.//from w ww . ja v a2 s .c o m * * @param properties The properties for the prompt in the XML. * * @return A mapping of property keys to their label/value pairs. * * @throws DomainException Thrown if a property is invalid. */ private static Map<String, LabelValuePair> getKeyValueLabelTrios(final String containerId, final Nodes properties) throws DomainException { int numProperties = properties.size(); if (numProperties == 0) { return Collections.emptyMap(); } Map<String, LabelValuePair> result = new HashMap<String, LabelValuePair>(numProperties); for (int i = 0; i < numProperties; i++) { Node propertyNode = properties.get(i); Nodes keys = propertyNode.query(XML_PROPERTY_KEY); if (keys.size() == 0) { throw new DomainException("The property key is missing: " + containerId); } else if (keys.size() > 1) { throw new DomainException("Multiple property keys were found: " + containerId); } String key = keys.get(0).getValue().trim(); if (key.length() == 0) { throw new DomainException("The property key cannot be whitespace only: " + containerId); } Nodes labels = propertyNode.query(XML_PROPERTY_LABEL); if (labels.size() == 0) { throw new DomainException("The property label is missing: " + containerId); } else if (labels.size() > 1) { throw new DomainException("Multiple property labels were found: " + containerId); } String label = labels.get(0).getValue().trim(); Number value = null; Nodes values = propertyNode.query(XML_PROPERTY_VALUE); if (values.size() > 1) { throw new DomainException("Multiple property values found: " + containerId); } else if (values.size() == 1) { String valueString = values.get(0).getValue().trim(); try { value = Short.decode(valueString); } catch (NumberFormatException notShort) { try { value = Integer.decode(valueString); } catch (NumberFormatException notInteger) { try { value = Long.decode(valueString); } catch (NumberFormatException notLong) { try { value = Float.parseFloat(valueString); } catch (NumberFormatException notFloat) { try { value = Double.parseDouble(valueString); } catch (NumberFormatException notDouble) { throw new DomainException( "The property value is not a numeric value: " + containerId); } } } } } } if (result.put(key, new LabelValuePair(label, value)) != null) { throw new DomainException( "Multiple properties with the same key were found for the container with id: " + containerId); } } return result; }
From source file:org.ohmage.domain.campaign.Campaign.java
/** * Processes an audio prompt and returns an AudioPrompt object. * /*from w w w.java 2 s . co m*/ * @param id * The prompt's unique identifier. * * @param condition * The condition value. * * @param unit * The prompt's visualization unit. * * @param text * The prompt's text value. * * @param explanationText * The prompt's explanation text value. * * @param skippable * Whether or not this prompt is skippable. * * @param skipLabel * The label to show to skip this prompt. * * @param displayLabel * The label for this display type. * * @param defaultValue * The default value given in the XML. * * @param properties * The properties defined in the XML for this prompt. * * @param index * The index of this prompt in its collection of survey items. * * @return An AudioPrompt object. * * @throws DomainException * Thrown if the required properties are missing or if any of the * parameters are invalid. */ private static AudioPrompt processAudio(final String id, final String condition, final String unit, final String text, final String explanationText, final boolean skippable, final String skipLabel, final String displayLabel, final String defaultValue, final Map<String, LabelValuePair> properties, final int index) throws DomainException { Long maxDuration = null; try { LabelValuePair maxDurationVlp = properties.get(AudioPrompt.XML_KEY_MAX_DURATION); if (maxDurationVlp != null) { maxDuration = Long.decode(maxDurationVlp.getLabel()); } } catch (NumberFormatException e) { throw new DomainException( "The '" + AudioPrompt.XML_KEY_MAX_DURATION + "' property is not an integer: " + id, e); } if (defaultValue != null) { throw new DomainException("Default values are not allowed for audio prompts: " + id); } return new AudioPrompt(id, condition, unit, text, explanationText, skippable, skipLabel, displayLabel, index, maxDuration); }
From source file:com.globalsight.webservices.Ambassador.java
/** * Add workflows of other languages to job * /*from w w w . j av a 2 s.c om*/ * @param p_accessToken * @param p_jobId * Job's id * @param p_wfInfos * Workflows of other languages * @return * @throws WebServiceException */ public String jobsAddLanguages(String p_accessToken, long p_jobId, String p_wfInfos) throws WebServiceException { try { Assert.assertNotEmpty(p_accessToken, "Access token"); Assert.assertNotEmpty(p_wfInfos, "Workflow of languages"); } catch (Exception e) { logger.error(e.getMessage(), e); throw new WebServiceException(e.getMessage()); } checkAccess(p_accessToken, "jobsAddLanguages"); checkPermission(p_accessToken, Permission.JOB_WORKFLOWS_ADD); WebServicesLog.Start activityStart = null; try { String userName = this.getUsernameFromSession(p_accessToken); Map<Object, Object> activityArgs = new HashMap<Object, Object>(); activityArgs.put("loggedUserName", userName); activityArgs.put("jobId", p_jobId); activityArgs.put("wfInfos", p_wfInfos); activityStart = WebServicesLog.start(Ambassador.class, "jobsAddLanguages(p_accessToken, p_jobId,p_wfInfos)", activityArgs); String[] wfInfoArray = p_wfInfos.split(","); WorkflowHandlerHelper.validateStateOfPagesByJobId(p_jobId); ArrayList wfInfos = new ArrayList(); for (int i = 0; i < wfInfoArray.length; i++) { wfInfos.add(Long.decode(wfInfoArray[i])); } WorkflowAdditionSender sender = new WorkflowAdditionSender(wfInfos, p_jobId); sender.sendToAddWorkflows(); } catch (Exception e) { logger.error(e.getMessage(), e); throw new WebServiceException(makeErrorXml("jobsSkipActivity", e.getMessage())); } finally { if (activityStart != null) { activityStart.end(); } } return null; }