Java Code Examples for com.helger.commons.state.ESuccess#FAILURE
The following examples show how to use
com.helger.commons.state.ESuccess#FAILURE .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ObjectPool.java From ph-commons with Apache License 2.0 | 6 votes |
@Nonnull public ESuccess returnObject (@Nonnull final DATATYPE aItem) { m_aLock.lock (); try { for (int i = 0; i < m_aItems.length; ++i) if (m_aUsed[i] && aItem == m_aItems[i]) { m_aUsed[i] = false; // Okay, we have one more unused item m_aAvailable.release (); return ESuccess.SUCCESS; } if (!isSilentMode ()) LOGGER.warn ("Object " + aItem + " is not pooled!"); return ESuccess.FAILURE; } finally { m_aLock.unlock (); } }
Example 2
Source File: JMXHelper.java From ph-commons with Apache License 2.0 | 6 votes |
@Nonnull public static ESuccess exposeMBean (@Nonnull final Object aObject, @Nonnull final ObjectName aObjectName) { ValueEnforcer.notNull (aObject, "Object"); ValueEnforcer.notNull (aObjectName, "ObjectName"); try { ManagementFactory.getPlatformMBeanServer ().registerMBean (aObject, aObjectName); return ESuccess.SUCCESS; } catch (final JMException ex) { LOGGER.error ("Error registering MBean with name " + aObjectName, ex); return ESuccess.FAILURE; } }
Example 3
Source File: BasicTreeItemWithID.java From ph-commons with Apache License 2.0 | 6 votes |
@Nonnull public final ESuccess changeParent (@Nonnull final ITEMTYPE aNewParent) { ValueEnforcer.notNull (aNewParent, "NewParent"); // no change so far if (getParent () == aNewParent) return ESuccess.SUCCESS; // cannot make a child of this, this' new parent. final ITEMTYPE aThis = _asT (this); if (aNewParent.isSameOrChildOf (aThis)) return ESuccess.FAILURE; // add this to the new parent if (m_aParent.removeChild (getID ()).isUnchanged ()) throw new IllegalStateException ("Failed to remove this from parent!"); // Remember new parent! m_aParent = aNewParent; return ESuccess.valueOfChange (aNewParent.internalAddChild (getID (), aThis, false)); }
Example 4
Source File: XMLMapHandler.java From ph-commons with Apache License 2.0 | 6 votes |
/** * Read a mapping from the passed input stream. * * @param aIS * The input stream to read from. May not be <code>null</code>. * @param aTargetMap * The target map to be filled. * @return {@link ESuccess#SUCCESS} if the stream could be opened, if it could * be read as XML and if the root element was correct. * {@link ESuccess#FAILURE} otherwise. */ @Nonnull public static ESuccess readMap (@Nonnull @WillClose final InputStream aIS, @Nonnull final Map <String, String> aTargetMap) { ValueEnforcer.notNull (aIS, "InputStream"); ValueEnforcer.notNull (aTargetMap, "TargetMap"); try (final InputStream aCloseMe = aIS) { // open file final IMicroDocument aDoc = MicroReader.readMicroXML (aIS); if (aDoc != null) { readMap (aDoc.getDocumentElement (), aTargetMap); return ESuccess.SUCCESS; } } catch (final Exception ex) { if (LOGGER.isWarnEnabled ()) LOGGER.warn ("Failed to read mapping resource '" + aIS + "'", ex); } return ESuccess.FAILURE; }
Example 5
Source File: GenericJAXBMarshaller.java From ph-commons with Apache License 2.0 | 6 votes |
@Nonnull public final ESuccess write (@Nonnull final JAXBTYPE aObject, @Nonnull final IJAXBMarshaller <JAXBTYPE> aMarshallerFunc) { ValueEnforcer.notNull (aObject, "Object"); ValueEnforcer.notNull (aMarshallerFunc, "MarshallerFunc"); try { final Marshaller aMarshaller = _createMarshaller (getClassLoader ()); customizeMarshaller (aMarshaller); final JAXBElement <JAXBTYPE> aJAXBElement = m_aJAXBElementWrapper.apply (aObject); aMarshallerFunc.doMarshal (aMarshaller, aJAXBElement); return ESuccess.SUCCESS; } catch (final JAXBException ex) { m_aWriteExceptionCallbacks.forEach (x -> x.onException (ex)); } return ESuccess.FAILURE; }
Example 6
Source File: XMLListHandler.java From ph-commons with Apache License 2.0 | 5 votes |
@Nonnull public static ESuccess readList (@Nonnull final IMicroElement aParentElement, @Nonnull final Collection <String> aTargetList) { ValueEnforcer.notNull (aParentElement, "ParentElement"); ValueEnforcer.notNull (aTargetList, "TargetList"); try { // and insert all elements for (final IMicroElement eItem : aParentElement.getAllChildElements (ELEMENT_ITEM)) { final String sValue = eItem.getAttributeValue (ATTR_VALUE); if (sValue == null) LOGGER.warn ("Ignoring list item because value is null"); else if (!aTargetList.add (sValue)) { if (LOGGER.isWarnEnabled ()) LOGGER.warn ("Ignoring list item '" + sValue + "' because value is already contained"); } } return ESuccess.SUCCESS; } catch (final Exception ex) { LOGGER.warn ("Failed to read list document", ex); } return ESuccess.FAILURE; }
Example 7
Source File: FileOperations.java From ph-commons with Apache License 2.0 | 5 votes |
/** * Copy the content of the source file to the destination file using * {@link InputStream} and {@link OutputStream}. * * @param aSrcFile * Source file. May not be <code>null</code>. * @param aDestFile * Destination file. May not be <code>null</code>. * @return {@link ESuccess} */ @Nonnull private static ESuccess _copyFileViaStreams (@Nonnull final File aSrcFile, @Nonnull final File aDestFile) { final InputStream aSrcIS = FileHelper.getInputStream (aSrcFile); if (aSrcIS == null) return ESuccess.FAILURE; try { final OutputStream aDstOS = FileHelper.getOutputStream (aDestFile, EAppend.TRUNCATE); if (aDstOS == null) return ESuccess.FAILURE; try { return StreamHelper.copyInputStreamToOutputStream (aSrcIS, aDstOS); } finally { StreamHelper.close (aDstOS); } } finally { StreamHelper.close (aSrcIS); } }
Example 8
Source File: SimpleFileIO.java From ph-commons with Apache License 2.0 | 5 votes |
@Nonnull public static ESuccess writeFile (@Nonnull final File aFile, @Nonnull final String sContent, @Nonnull final Charset aCharset) { final OutputStream aFOS = FileHelper.getOutputStream (aFile); return aFOS == null ? ESuccess.FAILURE : StreamHelper.writeStream (aFOS, sContent, aCharset); }
Example 9
Source File: AbstractWALDAO.java From ph-commons with Apache License 2.0 | 5 votes |
@Nonnull @MustBeLocked (ELockType.WRITE) private ESuccess _writeWALFile (@Nonnull @Nonempty final List <DATATYPE> aModifiedElements, @Nonnull final EDAOActionType eActionType, @Nonnull @Nonempty final String sWALFilename) { final FileSystemResource aWALRes = m_aIO.getResource (sWALFilename); try (final DataOutputStream aDOS = new DataOutputStream (aWALRes.getOutputStream (EAppend.APPEND))) { // Write action type ID StreamHelper.writeSafeUTF (aDOS, eActionType.getID ()); // Write number of elements aDOS.writeInt (aModifiedElements.size ()); // Write all data elements as XML Strings :) for (final DATATYPE aModifiedElement : aModifiedElements) { final String sElement = convertNativeToWALString (aModifiedElement); StreamHelper.writeSafeUTF (aDOS, sElement); } return ESuccess.SUCCESS; } catch (final Exception ex) { if (LOGGER.isErrorEnabled ()) LOGGER.error ("Error writing WAL file " + aWALRes, ex); triggerExceptionHandlersWrite (ex, sWALFilename, (IMicroDocument) null); } return ESuccess.FAILURE; }
Example 10
Source File: JsonReader.java From ph-commons with Apache License 2.0 | 5 votes |
/** * Generic JSON parse method. Usually this is not to be called manually - call * this only when you know what you are doing :) * * @param aReader * The reader to read from. Should be buffered. May not be * <code>null</code>. * @param aParserHandler * The parser handler. May not be <code>null</code>. * @param aCustomizeCallback * An optional {@link JsonParser} customization callback. May be * <code>null</code>. * @param aCustomExceptionCallback * A custom handler for unrecoverable errors. May be <code>null</code>. * @return {@link ESuccess} */ @Nonnull public static ESuccess parseJson (@Nonnull @WillClose final Reader aReader, @Nonnull final IJsonParserHandler aParserHandler, @Nullable final IJsonParserCustomizeCallback aCustomizeCallback, @Nullable final IJsonParseExceptionCallback aCustomExceptionCallback) { ValueEnforcer.notNull (aReader, "Reader"); ValueEnforcer.notNull (aParserHandler, "ParserHandler"); try { final JsonParser aParser = new JsonParser (aReader, aParserHandler); if (aCustomizeCallback != null) aCustomizeCallback.customizeJsonParser (aParser); aParser.parse (); return ESuccess.SUCCESS; } catch (final JsonParseException ex) { // Unrecoverable error if (aCustomExceptionCallback != null) aCustomExceptionCallback.onException (ex); else getDefaultParseExceptionCallback ().onException (ex); return ESuccess.FAILURE; } finally { StreamHelper.close (aReader); } }
Example 11
Source File: ChannelHelper.java From ph-commons with Apache License 2.0 | 5 votes |
@Nonnull public static ESuccess close (@Nullable final Channel aChannel) { if (aChannel != null && aChannel.isOpen ()) return StreamHelper.close (aChannel); return ESuccess.FAILURE; }
Example 12
Source File: SettingsPersistenceProperties.java From ph-commons with Apache License 2.0 | 5 votes |
@Nonnull public ESuccess writeSettings (@Nonnull final ISettings aSettings, @Nonnull @WillClose final OutputStream aOS) { ValueEnforcer.notNull (aOS, "OutputStream"); try { final NonBlockingProperties aProps = new NonBlockingProperties (); // Must not be sorted, as Properties sorts them as it wishes... for (final Map.Entry <String, Object> aEntry : aSettings.entrySet ()) { final String sName = aEntry.getKey (); final Object aValue = aEntry.getValue (); if (aValue instanceof ISettings) throw new IllegalArgumentException ("When saving settings to a Properties object, it may not contained nested settings! Now the key '" + sName + "' is mapped to a nested ISettings object!"); final String sValue = TypeConverter.convert (aValue, String.class); aProps.put (sName, sValue); } // Does not close the output stream! aProps.store (aOS, aSettings.getName ()); return ESuccess.SUCCESS; } catch (final IOException ex) { LOGGER.error ("Failed to write settings to properties file", ex); return ESuccess.FAILURE; } finally { StreamHelper.close (aOS); } }
Example 13
Source File: SettingsPersistenceJson.java From ph-commons with Apache License 2.0 | 5 votes |
@Nonnull public ESuccess writeSettings (@Nonnull final ISettings aSettings, @Nonnull @WillClose final OutputStream aOS) { ValueEnforcer.notNull (aOS, "OutputStream"); try { final IJsonObject aProps = new JsonObject (); for (final Map.Entry <String, Object> aEntry : CollectionHelper.getSorted (aSettings.entrySet (), Comparator.comparing (Map.Entry::getKey))) { final String sName = aEntry.getKey (); final Object aValue = aEntry.getValue (); final String sValue = TypeConverter.convert (aValue, String.class); aProps.add (sName, sValue); } final JsonWriterSettings aJWS = new JsonWriterSettings (); aJWS.setIndentEnabled (true); aJWS.setQuoteNames (false); // Does not close the output stream! new JsonWriter (aJWS).writeToWriterAndClose (aProps, StreamHelper.createWriter (aOS, m_aCharset)); return ESuccess.SUCCESS; } catch (final IOException ex) { LOGGER.error ("Failed to write settings to JSON file", ex); return ESuccess.FAILURE; } finally { StreamHelper.close (aOS); } }
Example 14
Source File: IFileRelativeIO.java From ph-commons with Apache License 2.0 | 5 votes |
/** * Helper function for saving a file with correct error handling. * * @param sRelativePath * name of the file. May not be <code>null</code>. * @param eAppend * Appending mode. May not be <code>null</code>. * @param aBytes * the bytes to be written. May not be <code>null</code>. * @return {@link ESuccess} */ @Nonnull default ESuccess writeFile (@Nonnull final String sRelativePath, @Nonnull final EAppend eAppend, @Nonnull final byte [] aBytes) { // save to file final OutputStream aOS = getOutputStream (sRelativePath, eAppend); if (aOS == null) return ESuccess.FAILURE; // Close the OS automatically! return StreamHelper.writeStream (aOS, aBytes); }
Example 15
Source File: IJAXBWriter.java From ph-commons with Apache License 2.0 | 5 votes |
/** * Write the passed object to an {@link IWritableResource}. * * @param aObject * The object to be written. May not be <code>null</code>. * @param aResource * The result resource to be written to. May not be <code>null</code>. * @return {@link ESuccess} */ @Nonnull default ESuccess write (@Nonnull final JAXBTYPE aObject, @Nonnull final IWritableResource aResource) { if (USE_JAXB_CHARSET_FIX) { final OutputStream aOS = aResource.getOutputStream (EAppend.TRUNCATE); if (aOS == null) return ESuccess.FAILURE; return write (aObject, aOS); } return write (aObject, TransformResultFactory.create (aResource)); }
Example 16
Source File: IJAXBWriter.java From ph-commons with Apache License 2.0 | 5 votes |
/** * Write the passed object to a {@link File}. * * @param aObject * The object to be written. May not be <code>null</code>. * @param aResultFile * The result file to be written to. May not be <code>null</code>. * @return {@link ESuccess} */ @Nonnull default ESuccess write (@Nonnull final JAXBTYPE aObject, @Nonnull final File aResultFile) { if (USE_JAXB_CHARSET_FIX) { final OutputStream aOS = FileHelper.getBufferedOutputStream (aResultFile); if (aOS == null) return ESuccess.FAILURE; return write (aObject, aOS); } return write (aObject, TransformResultFactory.create (aResultFile)); }
Example 17
Source File: StreamHelper.java From ph-commons with Apache License 2.0 | 4 votes |
/** * Pass the content of the given input stream to the given output stream. The * input stream is automatically closed, whereas the output stream stays open! * * @param aIS * The input stream to read from. May be <code>null</code>. * @param bCloseIS * <code>true</code> to close the InputStream, <code>false</code> to * leave it open. * @param aOS * The output stream to write to. May be <code>null</code>. * @param bCloseOS * <code>true</code> to close the OutputStream, <code>false</code> to * leave it open. * @param aBuffer * The buffer to use. May not be <code>null</code>. * @param aLimit * An optional maximum number of bytes to copied from the input stream * to the output stream. May be <code>null</code> to indicate no limit, * meaning all bytes are copied. * @param aExceptionCallback * The Exception callback to be invoked, if an exception occurs. May * not be <code>null</code>. * @param aCopyByteCount * An optional mutable long object that will receive the total number * of copied bytes. Note: and optional old value is overwritten. Note: * this is only called, if copying was successful, and not in case of * an exception. * @return <code>{@link ESuccess#SUCCESS}</code> if copying took place, <code> * {@link ESuccess#FAILURE}</code> otherwise * @since 9.3.6 */ @Nonnull public static ESuccess copyInputStreamToOutputStream (@Nullable final InputStream aIS, final boolean bCloseIS, @Nullable final OutputStream aOS, final boolean bCloseOS, @Nonnull @Nonempty final byte [] aBuffer, @Nullable final Long aLimit, @Nullable final IExceptionCallback <IOException> aExceptionCallback, @Nullable final MutableLong aCopyByteCount) { try { ValueEnforcer.notEmpty (aBuffer, "Buffer"); ValueEnforcer.isTrue (aLimit == null || aLimit.longValue () >= 0, () -> "Limit may not be negative: " + aLimit); if (aIS != null && aOS != null) { // both streams are not null final long nTotalBytesCopied; if (aLimit == null) nTotalBytesCopied = _copyInputStreamToOutputStream (aIS, aOS, aBuffer); else nTotalBytesCopied = _copyInputStreamToOutputStreamWithLimit (aIS, aOS, aBuffer, aLimit.longValue ()); // Add to statistics s_aByteSizeHdl.addSize (nTotalBytesCopied); // Remember copied bytes? if (aCopyByteCount != null) aCopyByteCount.set (nTotalBytesCopied); return ESuccess.SUCCESS; } } catch (final IOException ex) { if (aExceptionCallback != null) aExceptionCallback.onException (ex); else if (!isKnownEOFException (ex)) LOGGER.error ("Failed to copy from InputStream to OutputStream", _propagate (ex)); } finally { // Ensure streams are closed under all circumstances if (bCloseIS) close (aIS); if (bCloseOS) close (aOS); } return ESuccess.FAILURE; }
Example 18
Source File: StreamHelper.java From ph-commons with Apache License 2.0 | 4 votes |
/** * Pass the content of the given reader to the given writer. The reader is * automatically closed, whereas the writer stays open! * * @param aReader * The reader to read from. May be <code>null</code>. * @param bCloseReader * <code>true</code> to close the reader, <code>false</code> to keep it * open. * @param aWriter * The writer to write to. May be <code>null</code>. * @param bCloseWriter * <code>true</code> to close the writer, <code>false</code> to keep it * open. * @param aBuffer * The buffer to use. May not be <code>null</code>. * @param aLimit * An optional maximum number of chars to copied from the reader to the * writer. May be <code>null</code> to indicate no limit, meaning all * chars are copied. * @param aExceptionCallback * The Exception callback to be invoked, if an exception occurs. May * not be <code>null</code>. * @param aCopyCharCount * An optional mutable long object that will receive the total number * of copied characters. Note: and optional old value is overwritten! * @return <code>{@link ESuccess#SUCCESS}</code> if copying took place, <code> * {@link ESuccess#FAILURE}</code> otherwise */ @Nonnull public static ESuccess copyReaderToWriter (@Nullable final Reader aReader, final boolean bCloseReader, @Nullable final Writer aWriter, final boolean bCloseWriter, @Nonnull @Nonempty final char [] aBuffer, @Nullable final Long aLimit, @Nullable final IExceptionCallback <IOException> aExceptionCallback, @Nullable final MutableLong aCopyCharCount) { try { ValueEnforcer.notEmpty (aBuffer, "Buffer"); ValueEnforcer.isTrue (aLimit == null || aLimit.longValue () >= 0, () -> "Limit may not be negative: " + aLimit); if (aReader != null && aWriter != null) { // both streams are not null final long nTotalCharsCopied; if (aLimit == null) nTotalCharsCopied = _copyReaderToWriter (aReader, aWriter, aBuffer); else nTotalCharsCopied = _copyReaderToWriterWithLimit (aReader, aWriter, aBuffer, aLimit.longValue ()); // Add to statistics s_aCharSizeHdl.addSize (nTotalCharsCopied); // Remember number of copied characters if (aCopyCharCount != null) aCopyCharCount.set (nTotalCharsCopied); return ESuccess.SUCCESS; } } catch (final IOException ex) { if (aExceptionCallback != null) aExceptionCallback.onException (ex); else if (!isKnownEOFException (ex)) LOGGER.error ("Failed to copy from Reader to Writer", _propagate (ex)); } finally { // Ensure reader and writer are always closed if (bCloseReader) close (aReader); if (bCloseWriter) close (aWriter); } return ESuccess.FAILURE; }
Example 19
Source File: FileOperations.java From ph-commons with Apache License 2.0 | 4 votes |
/** * Copy the content of the source file to the destination file using * {@link FileChannel}. This version seems to fail with UNC paths. * * @param aSrcFile * Source file. May not be <code>null</code>. * @param aDestFile * Destination file. May not be <code>null</code>. * @return {@link ESuccess} */ @Nonnull private static ESuccess _copyFileViaChannel (@Nonnull final File aSrcFile, @Nonnull final File aDestFile) { final FileChannel aSrcChannel = FileChannelHelper.getFileReadChannel (aSrcFile); if (aSrcChannel == null) return ESuccess.FAILURE; try { final FileChannel aDstChannel = FileChannelHelper.getFileWriteChannel (aDestFile, EAppend.TRUNCATE); if (aDstChannel == null) return ESuccess.FAILURE; try { FileLock aSrcLock = null; FileLock aDestLock = null; try { final long nBytesToRead = aSrcChannel.size (); // Shared read lock and exclusive write lock aSrcLock = aSrcChannel.lock (0, nBytesToRead, true); aDestLock = aDstChannel.lock (); // Main copying - the loop version is much quicker than then // transferTo with full size! long nBytesWritten = 0; final long nChunkSize = 1L * CGlobal.BYTES_PER_MEGABYTE; while (nBytesWritten < nBytesToRead) nBytesWritten += aSrcChannel.transferTo (nBytesWritten, nChunkSize, aDstChannel); if (nBytesToRead != nBytesWritten) { if (LOGGER.isErrorEnabled ()) LOGGER.error ("Failed to copy file. Meant to read " + nBytesToRead + " bytes but wrote " + nBytesWritten); return ESuccess.FAILURE; } return ESuccess.SUCCESS; } catch (final IOException ex) { throw new IllegalStateException ("Failed to copy from " + aSrcFile + " to " + aDestFile, ex); } finally { // Unlock ChannelHelper.release (aDestLock); ChannelHelper.release (aSrcLock); } } finally { ChannelHelper.close (aDstChannel); } } finally { ChannelHelper.close (aSrcChannel); } }
Example 20
Source File: SimpleFileIO.java From ph-commons with Apache License 2.0 | 4 votes |
@Nonnull public static ESuccess writeFile (@Nonnull final File aFile, @Nonnull final byte [] aContent) { final OutputStream aFOS = FileHelper.getOutputStream (aFile); return aFOS == null ? ESuccess.FAILURE : StreamHelper.writeStream (aFOS, aContent); }