code
stringlengths 3
1.04M
| repo_name
stringlengths 5
109
| path
stringlengths 6
306
| language
stringclasses 1
value | license
stringclasses 15
values | size
int64 3
1.04M
|
---|---|---|---|---|---|
/*
* BridJ - Dynamic and blazing-fast native interop for Java.
* http://bridj.googlecode.com/
*
* Copyright (c) 2010-2015, Olivier Chafik (http://ochafik.com/)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Olivier Chafik nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY OLIVIER CHAFIK AND CONTRIBUTORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.bridj;
import static org.bridj.Pointer.allocate;
import static org.bridj.Pointer.allocateArray;
import java.util.AbstractList;
import java.util.Collection;
import java.util.RandomAccess;
import org.bridj.Pointer.ListType;
/**
* TODO : smart rewrite by chunks for removeAll and retainAll !
*
* @author ochafik
* @param <T> component type
*/
class DefaultNativeList<T> extends AbstractList<T> implements NativeList<T>, RandomAccess {
/*
* For optimization purposes, please look at AbstractList.java and AbstractCollection.java :
* http://www.koders.com/java/fidCFCB47A1819AB345234CC04B6A1EA7554C2C17C0.aspx?s=iso
* http://www.koders.com/java/fidA34BB0789922998CD34313EE49D61B06851A4397.aspx?s=iso
*
* We've reimplemented more methods than needed on purpose, for performance reasons (mainly using a native-optimized indexOf, that uses memmem and avoids deserializing too many elements)
*/
final ListType type;
final PointerIO<T> io;
volatile Pointer<T> pointer;
volatile long size;
public Pointer<?> getPointer() {
return pointer;
}
/**
* Create a native list that uses the provided storage and implementation
* strategy
*
* @param pointer
* @param type Implementation type
*/
DefaultNativeList(Pointer<T> pointer, ListType type) {
if (pointer == null || type == null) {
throw new IllegalArgumentException("Cannot build a " + getClass().getSimpleName() + " with " + pointer + " and " + type);
}
this.io = pointer.getIO("Cannot create a list out of untyped pointer " + pointer);
this.type = type;
this.size = pointer.getValidElements();
this.pointer = pointer;
}
protected void checkModifiable() {
if (type == ListType.Unmodifiable) {
throw new UnsupportedOperationException("This list is unmodifiable");
}
}
protected int safelyCastLongToInt(long i, String content) {
if (i > Integer.MAX_VALUE) {
throw new RuntimeException(content + " is bigger than Java int's maximum value : " + i);
}
return (int) i;
}
@Override
public int size() {
return safelyCastLongToInt(size, "Size of the native list");
}
@Override
public void clear() {
checkModifiable();
size = 0;
}
@Override
public T get(int i) {
if (i >= size || i < 0) {
throw new IndexOutOfBoundsException("Invalid index : " + i + " (list has size " + size + ")");
}
return pointer.get(i);
}
@Override
public T set(int i, T e) {
checkModifiable();
if (i >= size || i < 0) {
throw new IndexOutOfBoundsException("Invalid index : " + i + " (list has size " + size + ")");
}
T old = pointer.get(i);
pointer.set(i, e);
return old;
}
@SuppressWarnings("deprecation")
void add(long i, T e) {
checkModifiable();
if (i > size || i < 0) {
throw new IndexOutOfBoundsException("Invalid index : " + i + " (list has size " + size + ")");
}
requireSize(size + 1);
if (i < size) {
pointer.moveBytesAtOffsetTo(i, pointer, i + 1, size - i);
}
pointer.set(i, e);
size++;
}
@Override
public void add(int i, T e) {
add((long) i, e);
}
protected void requireSize(long newSize) {
if (newSize > pointer.getValidElements()) {
switch (type) {
case Dynamic:
long nextSize = newSize < 5 ? newSize + 1 : (long) (newSize * 1.6);
Pointer<T> newPointer = allocateArray(io, nextSize);
pointer.copyTo(newPointer);
pointer = newPointer;
break;
case FixedCapacity:
throw new UnsupportedOperationException("This list has a fixed capacity, cannot grow its storage");
case Unmodifiable:
// should not happen !
checkModifiable();
}
}
}
@SuppressWarnings("deprecation")
T remove(long i) {
checkModifiable();
if (i >= size || i < 0) {
throw new IndexOutOfBoundsException("Invalid index : " + i + " (list has size " + size + ")");
}
T old = pointer.get(i);
long targetSize = io.getTargetSize();
pointer.moveBytesAtOffsetTo((i + 1) * targetSize, pointer, i * targetSize, targetSize);
size--;
return old;
}
@Override
public T remove(int i) {
return remove((long) i);
}
@Override
public boolean remove(Object o) {
checkModifiable();
long i = indexOf(o, true, 0);
if (i < 0) {
return false;
}
remove(i);
return true;
}
@SuppressWarnings("unchecked")
long indexOf(Object o, boolean last, int offset) {
Pointer<T> pointer = this.pointer;
assert offset >= 0 && (last || offset > 0);
if (offset > 0) {
pointer = pointer.next(offset);
}
Pointer<T> needle = allocate(io);
needle.set((T) o);
Pointer<T> occurrence = last ? pointer.findLast(needle) : pointer.find(needle);
if (occurrence == null) {
return -1;
}
return occurrence.getPeer() - pointer.getPeer();
}
@Override
public int indexOf(Object o) {
return safelyCastLongToInt(indexOf(o, false, 0), "Index of the object");
}
@Override
public int lastIndexOf(Object o) {
return safelyCastLongToInt(indexOf(o, true, 0), "Last index of the object");
}
@Override
public boolean contains(Object o) {
return indexOf(o) >= 0;
}
@Override
public boolean addAll(int i, Collection<? extends T> clctn) {
if (i >= 0 && i < size) {
requireSize(size + clctn.size());
}
return super.addAll(i, clctn);
}
@Override
public Object[] toArray() {
return pointer.validElements(size).toArray();
}
@SuppressWarnings("hiding")
@Override
public <T> T[] toArray(T[] ts) {
return pointer.validElements(size).toArray(ts);
}
}
| nativelibs4java/BridJ | src/main/java/org/bridj/DefaultNativeList.java | Java | bsd-3-clause | 8,041 |
/*
* Copyright LWJGL. All rights reserved.
* License terms: https://www.lwjgl.org/license
* MACHINE GENERATED FILE, DO NOT EDIT
*/
package org.lwjgl.util.lz4;
import javax.annotation.*;
import java.nio.*;
import org.lwjgl.*;
import org.lwjgl.system.*;
import static org.lwjgl.system.Checks.*;
import static org.lwjgl.system.MemoryUtil.*;
import static org.lwjgl.util.lz4.LZ4HC.LZ4_STREAMHCSIZE_VOIDP;
/**
* <h3>Layout</h3>
*
* <pre><code>
* union LZ4_streamHC_t {
* size_t table[LZ4_STREAMHCSIZE_VOIDP];
* {@link LZ4HCCCtxInternal struct LZ4HC_CCtx_internal} internal_donotuse;
* }</code></pre>
*/
@NativeType("union LZ4_streamHC_t")
public class LZ4StreamHC extends Struct {
/** The struct size in bytes. */
public static final int SIZEOF;
/** The struct alignment in bytes. */
public static final int ALIGNOF;
/** The struct member offsets. */
public static final int
TABLE,
INTERNAL_DONOTUSE;
static {
Layout layout = __union(
__array(POINTER_SIZE, LZ4_STREAMHCSIZE_VOIDP),
__member(LZ4HCCCtxInternal.SIZEOF, LZ4HCCCtxInternal.ALIGNOF)
);
SIZEOF = layout.getSize();
ALIGNOF = layout.getAlignment();
TABLE = layout.offsetof(0);
INTERNAL_DONOTUSE = layout.offsetof(1);
}
/**
* Creates a {@code LZ4StreamHC} instance at the current position of the specified {@link ByteBuffer} container. Changes to the buffer's content will be
* visible to the struct instance and vice versa.
*
* <p>The created instance holds a strong reference to the container object.</p>
*/
public LZ4StreamHC(ByteBuffer container) {
super(memAddress(container), __checkContainer(container, SIZEOF));
}
@Override
public int sizeof() { return SIZEOF; }
/** @return a {@link PointerBuffer} view of the {@code table} field. */
@NativeType("size_t[LZ4_STREAMHCSIZE_VOIDP]")
public PointerBuffer table() { return ntable(address()); }
/** @return the value at the specified index of the {@code table} field. */
@NativeType("size_t")
public long table(int index) { return ntable(address(), index); }
/** @return a {@link LZ4HCCCtxInternal} view of the {@code internal_donotuse} field. */
@NativeType("struct LZ4HC_CCtx_internal")
public LZ4HCCCtxInternal internal_donotuse() { return ninternal_donotuse(address()); }
// -----------------------------------
/** Returns a new {@code LZ4StreamHC} instance for the specified memory address. */
public static LZ4StreamHC create(long address) {
return wrap(LZ4StreamHC.class, address);
}
/** Like {@link #create(long) create}, but returns {@code null} if {@code address} is {@code NULL}. */
@Nullable
public static LZ4StreamHC createSafe(long address) {
return address == NULL ? null : wrap(LZ4StreamHC.class, address);
}
/**
* Create a {@link LZ4StreamHC.Buffer} instance at the specified memory.
*
* @param address the memory address
* @param capacity the buffer capacity
*/
public static LZ4StreamHC.Buffer create(long address, int capacity) {
return wrap(Buffer.class, address, capacity);
}
/** Like {@link #create(long, int) create}, but returns {@code null} if {@code address} is {@code NULL}. */
@Nullable
public static LZ4StreamHC.Buffer createSafe(long address, int capacity) {
return address == NULL ? null : wrap(Buffer.class, address, capacity);
}
// -----------------------------------
/** Unsafe version of {@link #table}. */
public static PointerBuffer ntable(long struct) { return memPointerBuffer(struct + LZ4StreamHC.TABLE, LZ4_STREAMHCSIZE_VOIDP); }
/** Unsafe version of {@link #table(int) table}. */
public static long ntable(long struct, int index) {
return memGetAddress(struct + LZ4StreamHC.TABLE + check(index, LZ4_STREAMHCSIZE_VOIDP) * POINTER_SIZE);
}
/** Unsafe version of {@link #internal_donotuse}. */
public static LZ4HCCCtxInternal ninternal_donotuse(long struct) { return LZ4HCCCtxInternal.create(struct + LZ4StreamHC.INTERNAL_DONOTUSE); }
// -----------------------------------
/** An array of {@link LZ4StreamHC} structs. */
public static class Buffer extends StructBuffer<LZ4StreamHC, Buffer> {
private static final LZ4StreamHC ELEMENT_FACTORY = LZ4StreamHC.create(-1L);
/**
* Creates a new {@code LZ4StreamHC.Buffer} instance backed by the specified container.
*
* Changes to the container's content will be visible to the struct buffer instance and vice versa. The two buffers' position, limit, and mark values
* will be independent. The new buffer's position will be zero, its capacity and its limit will be the number of bytes remaining in this buffer divided
* by {@link LZ4StreamHC#SIZEOF}, and its mark will be undefined.
*
* <p>The created buffer instance holds a strong reference to the container object.</p>
*/
public Buffer(ByteBuffer container) {
super(container, container.remaining() / SIZEOF);
}
public Buffer(long address, int cap) {
super(address, null, -1, 0, cap, cap);
}
Buffer(long address, @Nullable ByteBuffer container, int mark, int pos, int lim, int cap) {
super(address, container, mark, pos, lim, cap);
}
@Override
protected Buffer self() {
return this;
}
@Override
protected LZ4StreamHC getElementFactory() {
return ELEMENT_FACTORY;
}
/** @return a {@link PointerBuffer} view of the {@code table} field. */
@NativeType("size_t[LZ4_STREAMHCSIZE_VOIDP]")
public PointerBuffer table() { return LZ4StreamHC.ntable(address()); }
/** @return the value at the specified index of the {@code table} field. */
@NativeType("size_t")
public long table(int index) { return LZ4StreamHC.ntable(address(), index); }
/** @return a {@link LZ4HCCCtxInternal} view of the {@code internal_donotuse} field. */
@NativeType("struct LZ4HC_CCtx_internal")
public LZ4HCCCtxInternal internal_donotuse() { return LZ4StreamHC.ninternal_donotuse(address()); }
}
} | LWJGL-CI/lwjgl3 | modules/lwjgl/lz4/src/generated/java/org/lwjgl/util/lz4/LZ4StreamHC.java | Java | bsd-3-clause | 6,358 |
package org.broadinstitute.hellbender.tools.spark.pathseq;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Output;
import htsjdk.samtools.SAMSequenceRecord;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.broadinstitute.hellbender.exceptions.UserException;
import org.broadinstitute.hellbender.utils.io.IOUtils;
import scala.Tuple2;
import java.io.*;
import java.util.*;
import java.util.zip.GZIPInputStream;
public final class PSBuildReferenceTaxonomyUtils {
protected static final Logger logger = LogManager.getLogger(PSBuildReferenceTaxonomyUtils.class);
private static final String VERTICAL_BAR_DELIMITER_REGEX = "\\s*\\|\\s*";
/**
* Build set of accessions contained in the reference.
* Returns: a map from accession to the name and length of the record. If the sequence name contains the
* taxonomic ID, it instead gets added to taxIdToProperties. Later we merge both results into taxIdToProperties.
* Method: First, look for either "taxid|<taxid>|" or "ref|<accession>|" in the sequence name. If neither of
* those are found, use the first word of the name as the accession.
*/
protected static Map<String, Tuple2<String, Long>> parseReferenceRecords(final List<SAMSequenceRecord> dictionaryList,
final Map<Integer, PSPathogenReferenceTaxonProperties> taxIdToProperties) {
final Map<String, Tuple2<String, Long>> accessionToNameAndLength = new HashMap<>();
for (final SAMSequenceRecord record : dictionaryList) {
final String recordName = record.getSequenceName();
final long recordLength = record.getSequenceLength();
final String[] tokens = recordName.split(VERTICAL_BAR_DELIMITER_REGEX);
String recordAccession = null;
int recordTaxId = PSTree.NULL_NODE;
for (int i = 0; i < tokens.length - 1 && recordTaxId == PSTree.NULL_NODE; i++) {
if (tokens[i].equals("ref")) {
recordAccession = tokens[i + 1];
} else if (tokens[i].equals("taxid")) {
recordTaxId = parseTaxonId(tokens[i + 1]);
}
}
if (recordTaxId == PSTree.NULL_NODE) {
if (recordAccession == null) {
final String[] tokens2 = tokens[0].split(" "); //Default accession to first word in the name
recordAccession = tokens2[0];
}
accessionToNameAndLength.put(recordAccession, new Tuple2<>(recordName, recordLength));
} else {
addReferenceAccessionToTaxon(recordTaxId, recordName, recordLength, taxIdToProperties);
}
}
return accessionToNameAndLength;
}
private static int parseTaxonId(final String taxonId) {
try {
return Integer.valueOf(taxonId);
} catch (final NumberFormatException e) {
throw new UserException.BadInput("Expected taxonomy ID to be an integer but found \"" + taxonId + "\"", e);
}
}
/**
* Helper classes for defining RefSeq and GenBank catalog formats. Columns should be given as 0-based indices.
*/
private interface AccessionCatalogFormat {
int getTaxIdColumn();
int getAccessionColumn();
}
private static final class RefSeqCatalogFormat implements AccessionCatalogFormat {
private static final int TAX_ID_COLUMN = 0;
private static final int ACCESSION_COLUMN = 2;
public int getTaxIdColumn() {
return TAX_ID_COLUMN;
}
public int getAccessionColumn() {
return ACCESSION_COLUMN;
}
}
private static final class GenBankCatalogFormat implements AccessionCatalogFormat {
private static final int TAX_ID_COLUMN = 6;
private static final int ACCESSION_COLUMN = 1;
public int getTaxIdColumn() {
return TAX_ID_COLUMN;
}
public int getAccessionColumn() {
return ACCESSION_COLUMN;
}
}
/**
* Builds maps of reference contig accessions to their taxonomic ids and vice versa.
* Input can be a RefSeq or Genbank catalog file. accNotFound is an initial list of
* accessions from the reference that have not been successfully looked up; if null,
* will be initialized to the accToRefInfo key set by default.
* <p>
* Returns a collection of reference accessions that could not be found, if any.
*/
protected static Set<String> parseCatalog(final BufferedReader reader,
final Map<String, Tuple2<String, Long>> accessionToNameAndLength,
final Map<Integer, PSPathogenReferenceTaxonProperties> taxIdToProperties,
final boolean bGenBank,
final Set<String> accessionsNotFoundIn) {
final Set<String> accessionsNotFoundOut;
try {
String line;
final AccessionCatalogFormat catalogFormat = bGenBank ? new GenBankCatalogFormat() : new RefSeqCatalogFormat();
final int taxIdColumnIndex = catalogFormat.getTaxIdColumn();
final int accessionColumnIndex = catalogFormat.getAccessionColumn();
if (accessionsNotFoundIn == null) {
//If accessionsNotFoundIn is null, this is the first call to parseCatalog, so initialize the set to all accessions
accessionsNotFoundOut = new HashSet<>(accessionToNameAndLength.keySet());
} else {
//Otherwise this is a subsequent call and we continue to look for any remaining accessions
accessionsNotFoundOut = new HashSet<>(accessionsNotFoundIn);
}
final int minColumns = Math.max(taxIdColumnIndex, accessionColumnIndex) + 1;
long lineNumber = 1;
while ((line = reader.readLine()) != null && !line.isEmpty()) {
final String[] tokens = line.trim().split("\t", minColumns + 1);
if (tokens.length >= minColumns) {
final int taxId = parseTaxonId(tokens[taxIdColumnIndex]);
final String accession = tokens[accessionColumnIndex];
if (accessionToNameAndLength.containsKey(accession)) {
final Tuple2<String, Long> nameAndLength = accessionToNameAndLength.get(accession);
addReferenceAccessionToTaxon(taxId, nameAndLength._1, nameAndLength._2, taxIdToProperties);
accessionsNotFoundOut.remove(accession);
}
} else {
throw new UserException.BadInput("Expected at least " + minColumns + " tab-delimited columns in " +
"GenBank catalog file, but only found " + tokens.length + " on line " + lineNumber);
}
lineNumber++;
}
} catch (final IOException e) {
throw new UserException.CouldNotReadInputFile("Error reading from catalog file", e);
}
return accessionsNotFoundOut;
}
/**
* Parses scientific name of each taxon and puts it in taxIdToProperties
*/
protected static void parseNamesFile(final BufferedReader reader, final Map<Integer, PSPathogenReferenceTaxonProperties> taxIdToProperties) {
try {
String line;
while ((line = reader.readLine()) != null) {
//Split into columns delimited by <TAB>|<TAB>
final String[] tokens = line.split(VERTICAL_BAR_DELIMITER_REGEX);
if (tokens.length < 4) {
throw new UserException.BadInput("Expected at least 4 columns in tax dump names file but found " + tokens.length);
}
final String nameType = tokens[3];
if (nameType.equals("scientific name")) {
final int taxId = parseTaxonId(tokens[0]);
final String name = tokens[1];
if (taxIdToProperties.containsKey(taxId)) {
taxIdToProperties.get(taxId).setName(name);
} else {
taxIdToProperties.put(taxId, new PSPathogenReferenceTaxonProperties(name));
}
}
}
} catch (final IOException e) {
throw new UserException.CouldNotReadInputFile("Error reading from taxonomy dump names file", e);
}
}
/**
* Gets the rank and parent of each taxon.
* Returns a Collection of tax ID's found in the nodes file that are not in taxIdToProperties (i.e. were not found in
* a reference sequence name using the taxid|\<taxid\> tag or the catalog file).
*/
protected static Collection<Integer> parseNodesFile(final BufferedReader reader, final Map<Integer, PSPathogenReferenceTaxonProperties> taxIdToProperties) {
try {
final Collection<Integer> taxIdsNotFound = new ArrayList<>();
String line;
while ((line = reader.readLine()) != null) {
final String[] tokens = line.split(VERTICAL_BAR_DELIMITER_REGEX);
if (tokens.length < 3) {
throw new UserException.BadInput("Expected at least 3 columns in tax dump nodes file but found " + tokens.length);
}
final int taxId = parseTaxonId(tokens[0]);
final int parent = parseTaxonId(tokens[1]);
final String rank = tokens[2];
final PSPathogenReferenceTaxonProperties taxonProperties;
if (taxIdToProperties.containsKey(taxId)) {
taxonProperties = taxIdToProperties.get(taxId);
} else {
taxonProperties = new PSPathogenReferenceTaxonProperties("tax_" + taxId);
taxIdsNotFound.add(taxId);
}
taxonProperties.setRank(rank);
if (taxId != PSTaxonomyConstants.ROOT_ID) { //keep root's parent set to null
taxonProperties.setParent(parent);
}
taxIdToProperties.put(taxId, taxonProperties);
}
return taxIdsNotFound;
} catch (final IOException e) {
throw new UserException.CouldNotReadInputFile("Error reading from taxonomy dump nodes file", e);
}
}
/**
* Helper function for building the map from tax id to reference contig accession
*/
private static void addReferenceAccessionToTaxon(final int taxId, final String accession, final long length, final Map<Integer, PSPathogenReferenceTaxonProperties> taxIdToProperties) {
taxIdToProperties.putIfAbsent(taxId, new PSPathogenReferenceTaxonProperties());
taxIdToProperties.get(taxId).addAccession(accession, length);
}
/**
* Removes nodes not in the tree from the tax_id-to-properties map
*/
static void removeUnusedTaxIds(final Map<Integer, PSPathogenReferenceTaxonProperties> taxIdToProperties,
final PSTree tree) {
taxIdToProperties.keySet().retainAll(tree.getNodeIDs());
}
/**
* Create reference_name-to-taxid map (just an inversion on taxIdToProperties)
*/
protected static Map<String, Integer> buildAccessionToTaxIdMap(final Map<Integer, PSPathogenReferenceTaxonProperties> taxIdToProperties,
final PSTree tree,
final int minNonVirusContigLength) {
final Map<String, Integer> accessionToTaxId = new HashMap<>();
for (final int taxId : taxIdToProperties.keySet()) {
final boolean isVirus = tree.getPathOf(taxId).contains(PSTaxonomyConstants.VIRUS_ID);
final PSPathogenReferenceTaxonProperties taxonProperties = taxIdToProperties.get(taxId);
for (final String name : taxonProperties.getAccessions()) {
if (isVirus || taxonProperties.getAccessionLength(name) >= minNonVirusContigLength) {
accessionToTaxId.put(name, taxId);
}
}
}
return accessionToTaxId;
}
/**
* Returns a PSTree representing a reduced taxonomic tree containing only taxa present in the reference
*/
protected static PSTree buildTaxonomicTree(final Map<Integer, PSPathogenReferenceTaxonProperties> taxIdToProperties) {
//Build tree of all taxa
final PSTree tree = new PSTree(PSTaxonomyConstants.ROOT_ID);
final Collection<Integer> invalidIds = new HashSet<>(taxIdToProperties.size());
for (final int taxId : taxIdToProperties.keySet()) {
if (taxId != PSTaxonomyConstants.ROOT_ID) {
final PSPathogenReferenceTaxonProperties taxonProperties = taxIdToProperties.get(taxId);
if (taxonProperties.getName() != null && taxonProperties.getParent() != PSTree.NULL_NODE && taxonProperties.getRank() != null) {
tree.addNode(taxId, taxonProperties.getName(), taxonProperties.getParent(), taxonProperties.getTotalLength(), taxonProperties.getRank());
} else {
invalidIds.add(taxId);
}
}
}
PSUtils.logItemizedWarning(logger, invalidIds, "The following taxonomic IDs did not have name/taxonomy information (this may happen when the catalog and taxdump files are inconsistent)");
final Set<Integer> unreachableNodes = tree.removeUnreachableNodes();
if (!unreachableNodes.isEmpty()) {
PSUtils.logItemizedWarning(logger, unreachableNodes, "Removed " + unreachableNodes.size() + " unreachable tree nodes");
}
tree.checkStructure();
//Trim tree down to nodes corresponding only to reference taxa (and their ancestors)
final Set<Integer> relevantNodes = new HashSet<>();
for (final int taxonId : taxIdToProperties.keySet()) {
if (!taxIdToProperties.get(taxonId).getAccessions().isEmpty() && tree.hasNode(taxonId)) {
relevantNodes.addAll(tree.getPathOf(taxonId));
}
}
if (relevantNodes.isEmpty()) {
throw new UserException.BadInput("Did not find any taxa corresponding to reference sequence names.\n\n"
+ "Check that reference names follow one of the required formats:\n\n"
+ "\t...|ref|<accession.version>|...\n"
+ "\t...|taxid|<taxonomy_id>|...\n"
+ "\t<accession.version><mask>...");
}
tree.retainNodes(relevantNodes);
return tree;
}
/**
* Gets a buffered reader for a gzipped file
* @param path File path
* @return Reader for the file
*/
public static BufferedReader getBufferedReaderGz(final String path) {
try {
return new BufferedReader(IOUtils.makeReaderMaybeGzipped(new File(path)));
} catch (final IOException e) {
throw new UserException.BadInput("Could not open file " + path, e);
}
}
/**
* Gets a Reader for a file in a gzipped tarball
* @param tarPath Path to the tarball
* @param fileName File within the tarball
* @return The file's reader
*/
public static BufferedReader getBufferedReaderTarGz(final String tarPath, final String fileName) {
try {
InputStream result = null;
final TarArchiveInputStream tarStream = new TarArchiveInputStream(new GZIPInputStream(new FileInputStream(tarPath)));
TarArchiveEntry entry = tarStream.getNextTarEntry();
while (entry != null) {
if (entry.getName().equals(fileName)) {
result = tarStream;
break;
}
entry = tarStream.getNextTarEntry();
}
if (result == null) {
throw new UserException.BadInput("Could not find file " + fileName + " in tarball " + tarPath);
}
return new BufferedReader(new InputStreamReader(result));
} catch (final IOException e) {
throw new UserException.BadInput("Could not open compressed tarball file " + fileName + " in " + tarPath, e);
}
}
/**
* Writes objects using Kryo to specified local file path.
* NOTE: using setReferences(false), which must also be set when reading the file. Does not work with nested
* objects that reference its parent.
*/
public static void writeTaxonomyDatabase(final String filePath, final PSTaxonomyDatabase taxonomyDatabase) {
try {
final Kryo kryo = new Kryo();
kryo.setReferences(false);
Output output = new Output(new FileOutputStream(filePath));
kryo.writeObject(output, taxonomyDatabase);
output.close();
} catch (final FileNotFoundException e) {
throw new UserException.CouldNotCreateOutputFile("Could not serialize objects to file", e);
}
}
}
| magicDGS/gatk | src/main/java/org/broadinstitute/hellbender/tools/spark/pathseq/PSBuildReferenceTaxonomyUtils.java | Java | bsd-3-clause | 17,481 |
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.content.browser.input;
import android.os.SystemClock;
import android.text.Editable;
import android.text.InputType;
import android.text.Selection;
import android.text.TextUtils;
import android.view.KeyCharacterMap;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.BaseInputConnection;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.ExtractedText;
import android.view.inputmethod.ExtractedTextRequest;
import org.chromium.base.Log;
import org.chromium.base.VisibleForTesting;
import org.chromium.blink_public.web.WebInputEventType;
import org.chromium.blink_public.web.WebTextInputFlags;
import org.chromium.ui.base.ime.TextInputType;
/**
* InputConnection is created by ContentView.onCreateInputConnection.
* It then adapts android's IME to chrome's RenderWidgetHostView using the
* native ImeAdapterAndroid via the class ImeAdapter.
*/
public class AdapterInputConnection extends BaseInputConnection {
private static final String TAG = "cr.InputConnection";
private static final boolean DEBUG = false;
/**
* Selection value should be -1 if not known. See EditorInfo.java for details.
*/
public static final int INVALID_SELECTION = -1;
public static final int INVALID_COMPOSITION = -1;
private final View mInternalView;
private final ImeAdapter mImeAdapter;
private final Editable mEditable;
private boolean mSingleLine;
private int mNumNestedBatchEdits = 0;
private int mPendingAccent;
private int mLastUpdateSelectionStart = INVALID_SELECTION;
private int mLastUpdateSelectionEnd = INVALID_SELECTION;
private int mLastUpdateCompositionStart = INVALID_COMPOSITION;
private int mLastUpdateCompositionEnd = INVALID_COMPOSITION;
@VisibleForTesting
AdapterInputConnection(View view, ImeAdapter imeAdapter, Editable editable,
EditorInfo outAttrs) {
super(view, true);
mInternalView = view;
mImeAdapter = imeAdapter;
mImeAdapter.setInputConnection(this);
mEditable = editable;
// The editable passed in might have been in use by a prior keyboard and could have had
// prior composition spans set. To avoid keyboard conflicts, remove all composing spans
// when taking ownership of an existing Editable.
finishComposingText();
mSingleLine = true;
outAttrs.imeOptions = EditorInfo.IME_FLAG_NO_FULLSCREEN
| EditorInfo.IME_FLAG_NO_EXTRACT_UI;
outAttrs.inputType = EditorInfo.TYPE_CLASS_TEXT
| EditorInfo.TYPE_TEXT_VARIATION_WEB_EDIT_TEXT;
int inputType = imeAdapter.getTextInputType();
int inputFlags = imeAdapter.getTextInputFlags();
if ((inputFlags & WebTextInputFlags.AutocompleteOff) != 0) {
outAttrs.inputType |= EditorInfo.TYPE_TEXT_FLAG_NO_SUGGESTIONS;
}
if (inputType == TextInputType.TEXT) {
// Normal text field
outAttrs.imeOptions |= EditorInfo.IME_ACTION_GO;
if ((inputFlags & WebTextInputFlags.AutocorrectOff) == 0) {
outAttrs.inputType |= EditorInfo.TYPE_TEXT_FLAG_AUTO_CORRECT;
}
} else if (inputType == TextInputType.TEXT_AREA
|| inputType == TextInputType.CONTENT_EDITABLE) {
outAttrs.inputType |= EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE;
if ((inputFlags & WebTextInputFlags.AutocorrectOff) == 0) {
outAttrs.inputType |= EditorInfo.TYPE_TEXT_FLAG_AUTO_CORRECT;
}
outAttrs.imeOptions |= EditorInfo.IME_ACTION_NONE;
mSingleLine = false;
} else if (inputType == TextInputType.PASSWORD) {
// Password
outAttrs.inputType = InputType.TYPE_CLASS_TEXT
| InputType.TYPE_TEXT_VARIATION_WEB_PASSWORD;
outAttrs.imeOptions |= EditorInfo.IME_ACTION_GO;
} else if (inputType == TextInputType.SEARCH) {
// Search
outAttrs.imeOptions |= EditorInfo.IME_ACTION_SEARCH;
} else if (inputType == TextInputType.URL) {
// Url
outAttrs.inputType = InputType.TYPE_CLASS_TEXT
| InputType.TYPE_TEXT_VARIATION_URI;
outAttrs.imeOptions |= EditorInfo.IME_ACTION_GO;
} else if (inputType == TextInputType.EMAIL) {
// Email
outAttrs.inputType = InputType.TYPE_CLASS_TEXT
| InputType.TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS;
outAttrs.imeOptions |= EditorInfo.IME_ACTION_GO;
} else if (inputType == TextInputType.TELEPHONE) {
// Telephone
// Number and telephone do not have both a Tab key and an
// action in default OSK, so set the action to NEXT
outAttrs.inputType = InputType.TYPE_CLASS_PHONE;
outAttrs.imeOptions |= EditorInfo.IME_ACTION_NEXT;
} else if (inputType == TextInputType.NUMBER) {
// Number
outAttrs.inputType = InputType.TYPE_CLASS_NUMBER
| InputType.TYPE_NUMBER_VARIATION_NORMAL
| InputType.TYPE_NUMBER_FLAG_DECIMAL;
outAttrs.imeOptions |= EditorInfo.IME_ACTION_NEXT;
}
// Handling of autocapitalize. Blink will send the flag taking into account the element's
// type. This is not using AutocapitalizeNone because Android does not autocapitalize by
// default and there is no way to express no capitalization.
// Autocapitalize is meant as a hint to the virtual keyboard.
if ((inputFlags & WebTextInputFlags.AutocapitalizeCharacters) != 0) {
outAttrs.inputType |= InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS;
} else if ((inputFlags & WebTextInputFlags.AutocapitalizeWords) != 0) {
outAttrs.inputType |= InputType.TYPE_TEXT_FLAG_CAP_WORDS;
} else if ((inputFlags & WebTextInputFlags.AutocapitalizeSentences) != 0) {
outAttrs.inputType |= InputType.TYPE_TEXT_FLAG_CAP_SENTENCES;
}
// Content editable doesn't use autocapitalize so we need to set it manually.
if (inputType == TextInputType.CONTENT_EDITABLE) {
outAttrs.inputType |= InputType.TYPE_TEXT_FLAG_CAP_SENTENCES;
}
outAttrs.initialSelStart = Selection.getSelectionStart(mEditable);
outAttrs.initialSelEnd = Selection.getSelectionEnd(mEditable);
mLastUpdateSelectionStart = outAttrs.initialSelStart;
mLastUpdateSelectionEnd = outAttrs.initialSelEnd;
if (DEBUG) Log.w(TAG, "Constructor called with outAttrs: " + outAttrs);
Selection.setSelection(mEditable, outAttrs.initialSelStart, outAttrs.initialSelEnd);
updateSelectionIfRequired();
}
/**
* Updates the AdapterInputConnection's internal representation of the text being edited and
* its selection and composition properties. The resulting Editable is accessible through the
* getEditable() method. If the text has not changed, this also calls updateSelection on the
* InputMethodManager.
*
* @param text The String contents of the field being edited.
* @param selectionStart The character offset of the selection start, or the caret position if
* there is no selection.
* @param selectionEnd The character offset of the selection end, or the caret position if there
* is no selection.
* @param compositionStart The character offset of the composition start, or -1 if there is no
* composition.
* @param compositionEnd The character offset of the composition end, or -1 if there is no
* selection.
* @param isNonImeChange True when the update was caused by non-IME (e.g. Javascript).
*/
@VisibleForTesting
public void updateState(String text, int selectionStart, int selectionEnd, int compositionStart,
int compositionEnd, boolean isNonImeChange) {
if (DEBUG) {
Log.w(TAG, "updateState [" + text + "] [" + selectionStart + " " + selectionEnd + "] ["
+ compositionStart + " " + compositionEnd + "] [" + isNonImeChange + "]");
}
// If this update is from the IME, no further state modification is necessary because the
// state should have been updated already by the IM framework directly.
if (!isNonImeChange) return;
// Non-breaking spaces can cause the IME to get confused. Replace with normal spaces.
text = text.replace('\u00A0', ' ');
selectionStart = Math.min(selectionStart, text.length());
selectionEnd = Math.min(selectionEnd, text.length());
compositionStart = Math.min(compositionStart, text.length());
compositionEnd = Math.min(compositionEnd, text.length());
String prevText = mEditable.toString();
boolean textUnchanged = prevText.equals(text);
if (!textUnchanged) {
mEditable.replace(0, mEditable.length(), text);
}
Selection.setSelection(mEditable, selectionStart, selectionEnd);
if (compositionStart == compositionEnd) {
removeComposingSpans(mEditable);
} else {
super.setComposingRegion(compositionStart, compositionEnd);
}
updateSelectionIfRequired();
}
/**
* @return Editable object which contains the state of current focused editable element.
*/
@Override
public Editable getEditable() {
return mEditable;
}
/**
* Sends selection update to the InputMethodManager unless we are currently in a batch edit or
* if the exact same selection and composition update was sent already.
*/
private void updateSelectionIfRequired() {
if (mNumNestedBatchEdits != 0) return;
int selectionStart = Selection.getSelectionStart(mEditable);
int selectionEnd = Selection.getSelectionEnd(mEditable);
int compositionStart = getComposingSpanStart(mEditable);
int compositionEnd = getComposingSpanEnd(mEditable);
// Avoid sending update if we sent an exact update already previously.
if (mLastUpdateSelectionStart == selectionStart
&& mLastUpdateSelectionEnd == selectionEnd
&& mLastUpdateCompositionStart == compositionStart
&& mLastUpdateCompositionEnd == compositionEnd) {
return;
}
if (DEBUG) {
Log.w(TAG, "updateSelectionIfRequired [" + selectionStart + " " + selectionEnd + "] ["
+ compositionStart + " " + compositionEnd + "]");
}
// updateSelection should be called every time the selection or composition changes
// if it happens not within a batch edit, or at the end of each top level batch edit.
getInputMethodManagerWrapper().updateSelection(
mInternalView, selectionStart, selectionEnd, compositionStart, compositionEnd);
mLastUpdateSelectionStart = selectionStart;
mLastUpdateSelectionEnd = selectionEnd;
mLastUpdateCompositionStart = compositionStart;
mLastUpdateCompositionEnd = compositionEnd;
}
/**
* @see BaseInputConnection#setComposingText(java.lang.CharSequence, int)
*/
@Override
public boolean setComposingText(CharSequence text, int newCursorPosition) {
if (DEBUG) Log.w(TAG, "setComposingText [" + text + "] [" + newCursorPosition + "]");
if (maybePerformEmptyCompositionWorkaround(text)) return true;
mPendingAccent = 0;
super.setComposingText(text, newCursorPosition);
updateSelectionIfRequired();
return mImeAdapter.checkCompositionQueueAndCallNative(text, newCursorPosition, false);
}
/**
* @see BaseInputConnection#commitText(java.lang.CharSequence, int)
*/
@Override
public boolean commitText(CharSequence text, int newCursorPosition) {
if (DEBUG) Log.w(TAG, "commitText [" + text + "] [" + newCursorPosition + "]");
if (maybePerformEmptyCompositionWorkaround(text)) return true;
mPendingAccent = 0;
super.commitText(text, newCursorPosition);
updateSelectionIfRequired();
return mImeAdapter.checkCompositionQueueAndCallNative(text, newCursorPosition,
text.length() > 0);
}
/**
* @see BaseInputConnection#performEditorAction(int)
*/
@Override
public boolean performEditorAction(int actionCode) {
if (DEBUG) Log.w(TAG, "performEditorAction [" + actionCode + "]");
if (actionCode == EditorInfo.IME_ACTION_NEXT) {
restartInput();
// Send TAB key event
long timeStampMs = SystemClock.uptimeMillis();
mImeAdapter.sendSyntheticKeyEvent(
WebInputEventType.RawKeyDown, timeStampMs, KeyEvent.KEYCODE_TAB, 0, 0);
} else {
mImeAdapter.sendKeyEventWithKeyCode(KeyEvent.KEYCODE_ENTER,
KeyEvent.FLAG_SOFT_KEYBOARD | KeyEvent.FLAG_KEEP_TOUCH_MODE
| KeyEvent.FLAG_EDITOR_ACTION);
}
return true;
}
/**
* @see BaseInputConnection#performContextMenuAction(int)
*/
@Override
public boolean performContextMenuAction(int id) {
if (DEBUG) Log.w(TAG, "performContextMenuAction [" + id + "]");
switch (id) {
case android.R.id.selectAll:
return mImeAdapter.selectAll();
case android.R.id.cut:
return mImeAdapter.cut();
case android.R.id.copy:
return mImeAdapter.copy();
case android.R.id.paste:
return mImeAdapter.paste();
default:
return false;
}
}
/**
* @see BaseInputConnection#getExtractedText(android.view.inputmethod.ExtractedTextRequest,
* int)
*/
@Override
public ExtractedText getExtractedText(ExtractedTextRequest request, int flags) {
if (DEBUG) Log.w(TAG, "getExtractedText");
ExtractedText et = new ExtractedText();
et.text = mEditable.toString();
et.partialEndOffset = mEditable.length();
et.selectionStart = Selection.getSelectionStart(mEditable);
et.selectionEnd = Selection.getSelectionEnd(mEditable);
et.flags = mSingleLine ? ExtractedText.FLAG_SINGLE_LINE : 0;
return et;
}
/**
* @see BaseInputConnection#beginBatchEdit()
*/
@Override
public boolean beginBatchEdit() {
if (DEBUG) Log.w(TAG, "beginBatchEdit [" + (mNumNestedBatchEdits == 0) + "]");
mNumNestedBatchEdits++;
return true;
}
/**
* @see BaseInputConnection#endBatchEdit()
*/
@Override
public boolean endBatchEdit() {
if (mNumNestedBatchEdits == 0) return false;
--mNumNestedBatchEdits;
if (DEBUG) Log.w(TAG, "endBatchEdit [" + (mNumNestedBatchEdits == 0) + "]");
if (mNumNestedBatchEdits == 0) updateSelectionIfRequired();
return mNumNestedBatchEdits != 0;
}
/**
* @see BaseInputConnection#deleteSurroundingText(int, int)
*/
@Override
public boolean deleteSurroundingText(int beforeLength, int afterLength) {
return deleteSurroundingTextImpl(beforeLength, afterLength, false);
}
/**
* Check if the given {@code index} is between UTF-16 surrogate pair.
* @param str The String.
* @param index The index
* @return True if the index is between UTF-16 surrogate pair, false otherwise.
*/
@VisibleForTesting
static boolean isIndexBetweenUtf16SurrogatePair(CharSequence str, int index) {
return index > 0 && index < str.length() && Character.isHighSurrogate(str.charAt(index - 1))
&& Character.isLowSurrogate(str.charAt(index));
}
private boolean deleteSurroundingTextImpl(
int beforeLength, int afterLength, boolean fromPhysicalKey) {
if (DEBUG) {
Log.w(TAG, "deleteSurroundingText [" + beforeLength + " " + afterLength + " "
+ fromPhysicalKey + "]");
}
if (mPendingAccent != 0) {
finishComposingText();
}
int originalBeforeLength = beforeLength;
int originalAfterLength = afterLength;
int selectionStart = Selection.getSelectionStart(mEditable);
int selectionEnd = Selection.getSelectionEnd(mEditable);
int availableBefore = selectionStart;
int availableAfter = mEditable.length() - selectionEnd;
beforeLength = Math.min(beforeLength, availableBefore);
afterLength = Math.min(afterLength, availableAfter);
// Adjust these values even before calling super.deleteSurroundingText() to be consistent
// with the super class.
if (isIndexBetweenUtf16SurrogatePair(mEditable, selectionStart - beforeLength)) {
beforeLength += 1;
}
if (isIndexBetweenUtf16SurrogatePair(mEditable, selectionEnd + afterLength)) {
afterLength += 1;
}
super.deleteSurroundingText(beforeLength, afterLength);
updateSelectionIfRequired();
// If this was called due to a physical key, no need to generate a key event here as
// the caller will take care of forwarding the original.
if (fromPhysicalKey) {
return true;
}
// For single-char deletion calls |ImeAdapter.sendKeyEventWithKeyCode| with the real key
// code. For multi-character deletion, executes deletion by calling
// |ImeAdapter.deleteSurroundingText| and sends synthetic key events with a dummy key code.
int keyCode = KeyEvent.KEYCODE_UNKNOWN;
if (originalBeforeLength == 1 && originalAfterLength == 0) {
keyCode = KeyEvent.KEYCODE_DEL;
} else if (originalBeforeLength == 0 && originalAfterLength == 1) {
keyCode = KeyEvent.KEYCODE_FORWARD_DEL;
}
boolean result = true;
if (keyCode == KeyEvent.KEYCODE_UNKNOWN) {
result = mImeAdapter.sendSyntheticKeyEvent(
WebInputEventType.RawKeyDown, SystemClock.uptimeMillis(), keyCode, 0, 0);
result &= mImeAdapter.deleteSurroundingText(beforeLength, afterLength);
result &= mImeAdapter.sendSyntheticKeyEvent(
WebInputEventType.KeyUp, SystemClock.uptimeMillis(), keyCode, 0, 0);
} else {
mImeAdapter.sendKeyEventWithKeyCode(
keyCode, KeyEvent.FLAG_SOFT_KEYBOARD | KeyEvent.FLAG_KEEP_TOUCH_MODE);
}
return result;
}
/**
* @see BaseInputConnection#sendKeyEvent(android.view.KeyEvent)
*/
@Override
public boolean sendKeyEvent(KeyEvent event) {
if (DEBUG) {
Log.w(TAG, "sendKeyEvent [" + event.getAction() + "] [" + event.getKeyCode() + "] ["
+ event.getUnicodeChar() + "]");
}
int action = event.getAction();
int keycode = event.getKeyCode();
int unicodeChar = event.getUnicodeChar();
// If this isn't a KeyDown event, no need to update composition state; just pass the key
// event through and return. But note that some keys, such as enter, may actually be
// handled on ACTION_UP in Blink.
if (action != KeyEvent.ACTION_DOWN) {
mImeAdapter.translateAndSendNativeEvents(event);
return true;
}
// If this is backspace/del or if the key has a character representation,
// need to update the underlying Editable (i.e. the local representation of the text
// being edited). Some IMEs like Jellybean stock IME and Samsung IME mix in delete
// KeyPress events instead of calling deleteSurroundingText.
if (keycode == KeyEvent.KEYCODE_DEL) {
deleteSurroundingTextImpl(1, 0, true);
} else if (keycode == KeyEvent.KEYCODE_FORWARD_DEL) {
deleteSurroundingTextImpl(0, 1, true);
} else if (keycode == KeyEvent.KEYCODE_ENTER) {
// Finish text composition when pressing enter, as that may submit a form field.
// TODO(aurimas): remove this workaround when crbug.com/278584 is fixed.
finishComposingText();
} else if ((unicodeChar & KeyCharacterMap.COMBINING_ACCENT) != 0) {
// Store a pending accent character and make it the current composition.
int pendingAccent = unicodeChar & KeyCharacterMap.COMBINING_ACCENT_MASK;
StringBuilder builder = new StringBuilder();
builder.appendCodePoint(pendingAccent);
setComposingText(builder.toString(), 1);
mPendingAccent = pendingAccent;
return true;
} else if (mPendingAccent != 0 && unicodeChar != 0) {
int combined = KeyEvent.getDeadChar(mPendingAccent, unicodeChar);
if (combined != 0) {
StringBuilder builder = new StringBuilder();
builder.appendCodePoint(combined);
commitText(builder.toString(), 1);
return true;
}
// Noncombinable character; commit the accent character and fall through to sending
// the key event for the character afterwards.
finishComposingText();
}
replaceSelectionWithUnicodeChar(unicodeChar);
mImeAdapter.translateAndSendNativeEvents(event);
return true;
}
/**
* Update the mEditable state to reflect what Blink will do in response to the KeyDown
* for a unicode-mapped key event.
* @param unicodeChar The Unicode character to update selection with.
*/
private void replaceSelectionWithUnicodeChar(int unicodeChar) {
if (unicodeChar == 0) return;
int selectionStart = Selection.getSelectionStart(mEditable);
int selectionEnd = Selection.getSelectionEnd(mEditable);
if (selectionStart > selectionEnd) {
int temp = selectionStart;
selectionStart = selectionEnd;
selectionEnd = temp;
}
mEditable.replace(selectionStart, selectionEnd, Character.toString((char) unicodeChar));
updateSelectionIfRequired();
}
/**
* @see BaseInputConnection#finishComposingText()
*/
@Override
public boolean finishComposingText() {
if (DEBUG) Log.w(TAG, "finishComposingText");
mPendingAccent = 0;
if (getComposingSpanStart(mEditable) == getComposingSpanEnd(mEditable)) {
return true;
}
super.finishComposingText();
updateSelectionIfRequired();
mImeAdapter.finishComposingText();
return true;
}
/**
* @see BaseInputConnection#setSelection(int, int)
*/
@Override
public boolean setSelection(int start, int end) {
if (DEBUG) Log.w(TAG, "setSelection [" + start + " " + end + "]");
int textLength = mEditable.length();
if (start < 0 || end < 0 || start > textLength || end > textLength) return true;
super.setSelection(start, end);
updateSelectionIfRequired();
return mImeAdapter.setEditableSelectionOffsets(start, end);
}
/**
* Informs the InputMethodManager and InputMethodSession (i.e. the IME) that the text
* state is no longer what the IME has and that it needs to be updated.
*/
void restartInput() {
if (DEBUG) Log.w(TAG, "restartInput");
getInputMethodManagerWrapper().restartInput(mInternalView);
mNumNestedBatchEdits = 0;
mPendingAccent = 0;
}
/**
* @see BaseInputConnection#setComposingRegion(int, int)
*/
@Override
public boolean setComposingRegion(int start, int end) {
if (DEBUG) Log.w(TAG, "setComposingRegion [" + start + " " + end + "]");
int textLength = mEditable.length();
int a = Math.min(start, end);
int b = Math.max(start, end);
if (a < 0) a = 0;
if (b < 0) b = 0;
if (a > textLength) a = textLength;
if (b > textLength) b = textLength;
if (a == b) {
removeComposingSpans(mEditable);
} else {
super.setComposingRegion(a, b);
}
updateSelectionIfRequired();
CharSequence regionText = null;
if (b > a) {
regionText = mEditable.subSequence(a, b);
}
return mImeAdapter.setComposingRegion(regionText, a, b);
}
boolean isActive() {
return getInputMethodManagerWrapper().isActive(mInternalView);
}
private InputMethodManagerWrapper getInputMethodManagerWrapper() {
return mImeAdapter.getInputMethodManagerWrapper();
}
/**
* This method works around the issue crbug.com/373934 where Blink does not cancel
* the composition when we send a commit with the empty text.
*
* TODO(aurimas) Remove this once crbug.com/373934 is fixed.
*
* @param text Text that software keyboard requested to commit.
* @return Whether the workaround was performed.
*/
private boolean maybePerformEmptyCompositionWorkaround(CharSequence text) {
int selectionStart = Selection.getSelectionStart(mEditable);
int selectionEnd = Selection.getSelectionEnd(mEditable);
int compositionStart = getComposingSpanStart(mEditable);
int compositionEnd = getComposingSpanEnd(mEditable);
if (TextUtils.isEmpty(text) && (selectionStart == selectionEnd)
&& compositionStart != INVALID_COMPOSITION
&& compositionEnd != INVALID_COMPOSITION) {
beginBatchEdit();
finishComposingText();
int selection = Selection.getSelectionStart(mEditable);
deleteSurroundingText(selection - compositionStart, selection - compositionEnd);
endBatchEdit();
return true;
}
return false;
}
@VisibleForTesting
static class ImeState {
public final String text;
public final int selectionStart;
public final int selectionEnd;
public final int compositionStart;
public final int compositionEnd;
public ImeState(String text, int selectionStart, int selectionEnd,
int compositionStart, int compositionEnd) {
this.text = text;
this.selectionStart = selectionStart;
this.selectionEnd = selectionEnd;
this.compositionStart = compositionStart;
this.compositionEnd = compositionEnd;
}
}
@VisibleForTesting
ImeState getImeStateForTesting() {
String text = mEditable.toString();
int selectionStart = Selection.getSelectionStart(mEditable);
int selectionEnd = Selection.getSelectionEnd(mEditable);
int compositionStart = getComposingSpanStart(mEditable);
int compositionEnd = getComposingSpanEnd(mEditable);
return new ImeState(text, selectionStart, selectionEnd, compositionStart, compositionEnd);
}
}
| PeterWangIntel/chromium-crosswalk | content/public/android/java/src/org/chromium/content/browser/input/AdapterInputConnection.java | Java | bsd-3-clause | 27,345 |
package com.hearthsim.card.basic.spell;
import com.hearthsim.card.spellcard.SpellTargetableCard;
import com.hearthsim.event.effect.EffectCharacter;
import com.hearthsim.event.effect.EffectCharacterBuffTemp;
import com.hearthsim.event.filter.FilterCharacter;
import com.hearthsim.event.filter.FilterCharacterTargetedSpell;
public class HeroicStrike extends SpellTargetableCard {
private final static EffectCharacter effect = new EffectCharacterBuffTemp(4);
/**
* Constructor
*
* Defaults to hasBeenUsed = false
*/
public HeroicStrike() {
super();
}
@Override
public FilterCharacter getTargetableFilter() {
return FilterCharacterTargetedSpell.SELF;
}
/**
* Heroic Strike
*
* Gives the hero +4 attack this turn
*
*
*
* @param side
* @param boardState The BoardState before this card has performed its action. It will be manipulated and returned.
*
* @return The boardState is manipulated and returned
*/
@Override
public EffectCharacter getTargetableEffect() {
return HeroicStrike.effect;
}
}
| oyachai/HearthSim | src/main/java/com/hearthsim/card/basic/spell/HeroicStrike.java | Java | mit | 1,140 |
package org.multibit.hd.core.events;
/**
* <p>Signature interface to provide the following to Core Event API:</p>
* <ul>
* <li>Identification of core events</li>
* </ul>
* <p>A core event should be named using a noun as the first part of the name (e.g. ExchangeRateChangedEvent)</p>
* <p>A core event can occur at any time and will not be synchronized with other events.</p>
*
* @since 0.0.1
*
*/
public interface CoreEvent {
}
| oscarguindzberg/multibit-hd | mbhd-core/src/main/java/org/multibit/hd/core/events/CoreEvent.java | Java | mit | 449 |
/* This file was generated by SableCC (http://www.sablecc.org/). */
package com.bju.cps450.node;
import com.bju.cps450.analysis.*;
@SuppressWarnings("nls")
public final class TPlus extends Token
{
public TPlus()
{
super.setText("+");
}
public TPlus(int line, int pos)
{
super.setText("+");
setLine(line);
setPos(pos);
}
@Override
public Object clone()
{
return new TPlus(getLine(), getPos());
}
@Override
public void apply(Switch sw)
{
((Analysis) sw).caseTPlus(this);
}
@Override
public void setText(@SuppressWarnings("unused") String text)
{
throw new RuntimeException("Cannot change TPlus text.");
}
}
| asdfzt/CPS450-MiniJava | MiniJavaParserWithAST/gen-src/com/bju/cps450/node/TPlus.java | Java | mit | 738 |
/*
* @(#)LayouterSample.java
*
* Copyright (c) 1996-2010 by the original authors of JHotDraw and all its
* contributors. All rights reserved.
*
* You may not use, copy or modify this file, except in compliance with the
* license agreement you entered into with the copyright holders. For details
* see accompanying license terms.
*/
package org.jhotdraw.samples.mini;
import org.jhotdraw.draw.tool.DelegationSelectionTool;
import org.jhotdraw.draw.layouter.VerticalLayouter;
import javax.swing.*;
import org.jhotdraw.draw.*;
/**
* Example showing how to layout two editable text figures and a line figure
* within a graphical composite figure.
*
* @author Werner Randelshofer
* @version $Id: LayouterSample.java 718 2010-11-21 17:49:53Z rawcoder $
*/
public class LayouterSample {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
// Create a graphical composite figure.
GraphicalCompositeFigure composite = new GraphicalCompositeFigure();
// Add child figures to the composite figure
composite.add(new TextFigure("Above the line"));
composite.add(new LineFigure());
composite.add(new TextFigure("Below the line"));
// Set a layouter and perform the layout
composite.setLayouter(new VerticalLayouter());
composite.layout();
// Add the composite figure to a drawing
Drawing drawing = new DefaultDrawing();
drawing.add(composite);
// Create a frame with a drawing view and a drawing editor
JFrame f = new JFrame("My Drawing");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(400, 300);
DrawingView view = new DefaultDrawingView();
view.setDrawing(drawing);
f.getContentPane().add(view.getComponent());
DrawingEditor editor = new DefaultDrawingEditor();
editor.add(view);
editor.setTool(new DelegationSelectionTool());
f.setVisible(true);
}
});
}
}
| ahmedvc/umple | Umplificator/UmplifiedProjects/jhotdraw7/src/main/java/org/jhotdraw/samples/mini/LayouterSample.java | Java | mit | 2,276 |
package com.iluwatar.front.controller;
/**
*
* Command for archers.
*
*/
public class ArcherCommand implements Command {
@Override
public void process() {
new ArcherView().display();
}
}
| dlee0113/java-design-patterns | front-controller/src/main/java/com/iluwatar/front/controller/ArcherCommand.java | Java | mit | 204 |
//
import java.util.Comparator;
abc.sort(Comparator.naturalOrder());
//
| general-language-syntax/GLS | test/integration/ListSortStrings/list sort strings.java | Java | mit | 73 |
package org.multibit.hd.ui.views.wizards.appearance_settings;
import com.google.common.base.Optional;
import org.multibit.hd.ui.views.wizards.AbstractWizard;
import org.multibit.hd.ui.views.wizards.AbstractWizardPanelView;
import java.util.Map;
/**
* <p>Wizard to provide the following to UI for "appearance" wizard:</p>
* <ol>
* <li>Enter details</li>
* </ol>
*
* @since 0.0.1
*
*/
public class AppearanceSettingsWizard extends AbstractWizard<AppearanceSettingsWizardModel> {
public AppearanceSettingsWizard(AppearanceSettingsWizardModel model) {
super(model, false, Optional.absent());
}
@Override
protected void populateWizardViewMap(Map<String, AbstractWizardPanelView> wizardViewMap) {
// Use the wizard parameter to retrieve the appropriate mode
wizardViewMap.put(
AppearanceSettingsState.APPEARANCE_ENTER_DETAILS.name(),
new AppearanceSettingsPanelView(this, AppearanceSettingsState.APPEARANCE_ENTER_DETAILS.name())
);
}
}
| oscarguindzberg/multibit-hd | mbhd-swing/src/main/java/org/multibit/hd/ui/views/wizards/appearance_settings/AppearanceSettingsWizard.java | Java | mit | 984 |
/*
* Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
package org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.toxml;
import com.google.common.base.Preconditions;
import org.opendaylight.controller.netconf.confignetconfconnector.util.Util;
import org.w3c.dom.Document;
import java.util.List;
import java.util.Map;
public class SimpleUnionAttributeWritingStrategy extends SimpleAttributeWritingStrategy {
/**
* @param document
* @param key
*/
public SimpleUnionAttributeWritingStrategy(Document document, String key) {
super(document, key);
}
protected Object preprocess(Object value) {
Util.checkType(value, Map.class);
Preconditions.checkArgument(((Map)value).size() == 1, "Unexpected number of values in %s, expected 1", value);
Object listOfStrings = ((Map) value).values().iterator().next();
Util.checkType(listOfStrings, List.class);
StringBuilder b = new StringBuilder();
for (Object character: (List)listOfStrings) {
Util.checkType(character, String.class);
b.append(character);
}
return b.toString();
}
}
| aryantaheri/controller | opendaylight/netconf/config-netconf-connector/src/main/java/org/opendaylight/controller/netconf/confignetconfconnector/mapping/attributes/toxml/SimpleUnionAttributeWritingStrategy.java | Java | epl-1.0 | 1,436 |
/*******************************************************************************
* Copyright (c) 2006, 2009 David A Carlson
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* David A Carlson (XMLmodeling.com) - initial API and implementation
*******************************************************************************/
package org.openhealthtools.mdht.emf.w3c.xhtml.internal.impl;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.impl.ENotificationImpl;
import org.openhealthtools.mdht.emf.w3c.xhtml.Code;
import org.openhealthtools.mdht.emf.w3c.xhtml.MifClassType;
import org.openhealthtools.mdht.emf.w3c.xhtml.StyleSheet;
import org.openhealthtools.mdht.emf.w3c.xhtml.XhtmlPackage;
/**
* <!-- begin-user-doc -->
* An implementation of the model object '<em><b>Code</b></em>'.
* <!-- end-user-doc -->
* <p>
* The following features are implemented:
* <ul>
* <li>{@link org.openhealthtools.mdht.emf.w3c.xhtml.internal.impl.CodeImpl#getClass_ <em>Class</em>}</li>
* <li>{@link org.openhealthtools.mdht.emf.w3c.xhtml.internal.impl.CodeImpl#getLang <em>Lang</em>}</li>
* <li>{@link org.openhealthtools.mdht.emf.w3c.xhtml.internal.impl.CodeImpl#getStyle <em>Style</em>}</li>
* </ul>
* </p>
*
* @generated
*/
public class CodeImpl extends InlineImpl implements Code {
/**
* The default value of the '{@link #getClass_() <em>Class</em>}' attribute.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @see #getClass_()
* @generated
* @ordered
*/
protected static final MifClassType CLASS_EDEFAULT = MifClassType.INSERTED;
/**
* The cached value of the '{@link #getClass_() <em>Class</em>}' attribute.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @see #getClass_()
* @generated
* @ordered
*/
protected MifClassType class_ = CLASS_EDEFAULT;
/**
* This is true if the Class attribute has been set.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
* @ordered
*/
protected boolean classESet;
/**
* The default value of the '{@link #getLang() <em>Lang</em>}' attribute.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @see #getLang()
* @generated
* @ordered
*/
protected static final String LANG_EDEFAULT = null;
/**
* The cached value of the '{@link #getLang() <em>Lang</em>}' attribute.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @see #getLang()
* @generated
* @ordered
*/
protected String lang = LANG_EDEFAULT;
/**
* The default value of the '{@link #getStyle() <em>Style</em>}' attribute.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @see #getStyle()
* @generated
* @ordered
*/
protected static final StyleSheet STYLE_EDEFAULT = StyleSheet.REQUIREMENT;
/**
* The cached value of the '{@link #getStyle() <em>Style</em>}' attribute.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @see #getStyle()
* @generated
* @ordered
*/
protected StyleSheet style = STYLE_EDEFAULT;
/**
* This is true if the Style attribute has been set.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
* @ordered
*/
protected boolean styleESet;
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
*/
protected CodeImpl() {
super();
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
*/
@Override
protected EClass eStaticClass() {
return XhtmlPackage.Literals.CODE;
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
*/
public MifClassType getClass_() {
return class_;
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
*/
public void setClass(MifClassType newClass) {
MifClassType oldClass = class_;
class_ = newClass == null
? CLASS_EDEFAULT
: newClass;
boolean oldClassESet = classESet;
classESet = true;
if (eNotificationRequired()) {
eNotify(new ENotificationImpl(
this, Notification.SET, XhtmlPackage.CODE__CLASS, oldClass, class_, !oldClassESet));
}
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
*/
public void unsetClass() {
MifClassType oldClass = class_;
boolean oldClassESet = classESet;
class_ = CLASS_EDEFAULT;
classESet = false;
if (eNotificationRequired()) {
eNotify(new ENotificationImpl(
this, Notification.UNSET, XhtmlPackage.CODE__CLASS, oldClass, CLASS_EDEFAULT, oldClassESet));
}
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
*/
public boolean isSetClass() {
return classESet;
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
*/
public String getLang() {
return lang;
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
*/
public void setLang(String newLang) {
String oldLang = lang;
lang = newLang;
if (eNotificationRequired()) {
eNotify(new ENotificationImpl(this, Notification.SET, XhtmlPackage.CODE__LANG, oldLang, lang));
}
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
*/
public StyleSheet getStyle() {
return style;
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
*/
public void setStyle(StyleSheet newStyle) {
StyleSheet oldStyle = style;
style = newStyle == null
? STYLE_EDEFAULT
: newStyle;
boolean oldStyleESet = styleESet;
styleESet = true;
if (eNotificationRequired()) {
eNotify(new ENotificationImpl(
this, Notification.SET, XhtmlPackage.CODE__STYLE, oldStyle, style, !oldStyleESet));
}
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
*/
public void unsetStyle() {
StyleSheet oldStyle = style;
boolean oldStyleESet = styleESet;
style = STYLE_EDEFAULT;
styleESet = false;
if (eNotificationRequired()) {
eNotify(new ENotificationImpl(
this, Notification.UNSET, XhtmlPackage.CODE__STYLE, oldStyle, STYLE_EDEFAULT, oldStyleESet));
}
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
*/
public boolean isSetStyle() {
return styleESet;
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
*/
@Override
public Object eGet(int featureID, boolean resolve, boolean coreType) {
switch (featureID) {
case XhtmlPackage.CODE__CLASS:
return getClass_();
case XhtmlPackage.CODE__LANG:
return getLang();
case XhtmlPackage.CODE__STYLE:
return getStyle();
}
return super.eGet(featureID, resolve, coreType);
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
*/
@Override
public void eSet(int featureID, Object newValue) {
switch (featureID) {
case XhtmlPackage.CODE__CLASS:
setClass((MifClassType) newValue);
return;
case XhtmlPackage.CODE__LANG:
setLang((String) newValue);
return;
case XhtmlPackage.CODE__STYLE:
setStyle((StyleSheet) newValue);
return;
}
super.eSet(featureID, newValue);
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
*/
@Override
public void eUnset(int featureID) {
switch (featureID) {
case XhtmlPackage.CODE__CLASS:
unsetClass();
return;
case XhtmlPackage.CODE__LANG:
setLang(LANG_EDEFAULT);
return;
case XhtmlPackage.CODE__STYLE:
unsetStyle();
return;
}
super.eUnset(featureID);
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
*/
@Override
public boolean eIsSet(int featureID) {
switch (featureID) {
case XhtmlPackage.CODE__CLASS:
return isSetClass();
case XhtmlPackage.CODE__LANG:
return LANG_EDEFAULT == null
? lang != null
: !LANG_EDEFAULT.equals(lang);
case XhtmlPackage.CODE__STYLE:
return isSetStyle();
}
return super.eIsSet(featureID);
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
*/
@Override
public String toString() {
if (eIsProxy()) {
return super.toString();
}
StringBuffer result = new StringBuffer(super.toString());
result.append(" (class: ");
if (classESet) {
result.append(class_);
} else {
result.append("<unset>");
}
result.append(", lang: ");
result.append(lang);
result.append(", style: ");
if (styleESet) {
result.append(style);
} else {
result.append("<unset>");
}
result.append(')');
return result.toString();
}
} // CodeImpl
| drbgfc/mdht | hl7/plugins/org.openhealthtools.mdht.emf.hl7.mif2/src/org/openhealthtools/mdht/emf/w3c/xhtml/internal/impl/CodeImpl.java | Java | epl-1.0 | 8,990 |
/*******************************************************************************
* Copyright (c) 2005-2010 VecTrace (Zingo Andersen) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* John Peberdy implementation
*******************************************************************************/
package com.vectrace.MercurialEclipse.commands;
import java.io.File;
import java.util.List;
import org.eclipse.core.runtime.Assert;
/**
* A command to invoke hg definitely outside of an hg root.
*/
public class RootlessHgCommand extends AbstractShellCommand {
public RootlessHgCommand(String command, String uiName) {
this(command, uiName, null);
}
public RootlessHgCommand(String command, String uiName, File workingDir) {
super(uiName, null, workingDir, false);
Assert.isNotNull(command);
this.command = command;
}
// operations
/**
* @see com.vectrace.MercurialEclipse.commands.AbstractShellCommand#customizeCommands(java.util.List)
*/
@Override
protected void customizeCommands(List<String> cmd) {
cmd.add(1, "-y");
}
/**
* @see com.vectrace.MercurialEclipse.commands.AbstractShellCommand#getExecutable()
*/
@Override
protected String getExecutable() {
return HgClients.getExecutable();
}
}
| boa0332/mercurialeclipse | plugin/src/com/vectrace/MercurialEclipse/commands/RootlessHgCommand.java | Java | epl-1.0 | 1,464 |
/*******************************************************************************
* This file is part of OpenNMS(R).
*
* Copyright (C) 2011-2012 The OpenNMS Group, Inc.
* OpenNMS(R) is Copyright (C) 1999-2012 The OpenNMS Group, Inc.
*
* OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
*
* OpenNMS(R) is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* OpenNMS(R) is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenNMS(R). If not, see:
* http://www.gnu.org/licenses/
*
* For more information contact:
* OpenNMS(R) Licensing <license@opennms.org>
* http://www.opennms.org/
* http://www.opennms.com/
*******************************************************************************/
package org.opennms.smoketest;
import org.junit.Before;
import org.junit.Test;
public class ChartsPageTest extends OpenNMSSeleniumTestCase {
@Before
public void setUp() throws Exception {
super.setUp();
clickAndWait("link=Charts");
}
@Test
public void testChartsPage() throws Exception {
waitForText("Charts");
waitForElement("css=img[alt=sample-bar-chart]");
waitForElement("css=img[alt=sample-bar-chart2]");
waitForElement("css=img[alt=sample-bar-chart3]");
}
}
| rfdrake/opennms | smoke-test/src/test/java/org/opennms/smoketest/ChartsPageTest.java | Java | gpl-2.0 | 1,723 |
/*
* This file is part of JPhotoAlbum.
* Copyright 2004 Jari Karjala <jpkware.com> & Tarja Hakala <hakalat.net>
*
* @version $Id: JPhotoDirectory.java,v 1.1.1.1 2004/05/21 18:24:59 jkarjala Exp $
*/
/** Container for a single photo, may not always contain a real photo, but
* just text element.
* @see JPhotoAlbumLink.java
*/
package fi.iki.jka;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.InputStream;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.FileCacheImageInputStream;
import com.drew.metadata.exif.ExifDirectory;
public class JPhotoDirectory extends JPhoto {
public JPhotoDirectory() {
this(defaultOwner, null);
}
public JPhotoDirectory(JPhotoCollection owner) {
this(owner, null);
}
public JPhotoDirectory(File original) {
this(defaultOwner, original);
}
public JPhotoDirectory(JPhotoCollection owner, File original) {
super(owner, original);
}
public BufferedImage getThumbImage() {
InputStream ins = getClass().getClassLoader().getResourceAsStream("pics/directory.png");
Iterator readers = ImageIO.getImageReadersBySuffix("png");
ImageReader imageReader = (ImageReader)readers.next();
BufferedImage thumb = null;
try {
imageReader.setInput(new FileCacheImageInputStream(ins, null));
thumb = imageReader.read(0);
ins.close();
}
catch (Exception e) {
System.out.println("getThumbImage:"+e);
}
imageReader.dispose();
return thumb;
}
public BufferedImage getCachedThumb() {
return getThumbImage();
}
public BufferedImage getThumbImageAsync() {
return getThumbImage();
}
public BufferedImage getFullImage() {
return null;
}
public ExifDirectory getExifDirectory() {
return null;
}
public String toString() {
return "Directory='"+getImageName()+"'";
}
}
| 91wzhang/sei-jphotoalbum | src/fi/iki/jka/JPhotoDirectory.java | Java | gpl-2.0 | 2,070 |
/*
* PS3 Media Server, for streaming any medias to your PS3.
* Copyright (C) 2008 A.Brochard
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; version 2
* of the License only.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package net.pms.encoders;
import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.factories.Borders;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import java.awt.ComponentOrientation;
import java.awt.Font;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Locale;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JPanel;
import net.pms.Messages;
import net.pms.PMS;
import net.pms.configuration.DeviceConfiguration;
import net.pms.configuration.PmsConfiguration;
import net.pms.configuration.RendererConfiguration;
import net.pms.dlna.*;
import net.pms.formats.Format;
import net.pms.io.*;
import net.pms.newgui.GuiUtil;
import net.pms.util.CodecUtil;
import net.pms.util.FormLayoutUtil;
import net.pms.util.PlayerUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class TsMuxeRVideo extends Player {
private static final Logger LOGGER = LoggerFactory.getLogger(TsMuxeRVideo.class);
private static final String COL_SPEC = "left:pref, 0:grow";
private static final String ROW_SPEC = "p, 3dlu, p, 3dlu, p, 3dlu, p, 3dlu, p, 3dlu, 0:grow";
public static final String ID = "tsmuxer";
@Deprecated
public TsMuxeRVideo(PmsConfiguration configuration) {
this();
}
public TsMuxeRVideo() {
}
@Override
public boolean excludeFormat(Format format) {
String extension = format.getMatchedExtension();
return extension != null
&& !extension.equals("mp4")
&& !extension.equals("mkv")
&& !extension.equals("ts")
&& !extension.equals("tp")
&& !extension.equals("m2ts")
&& !extension.equals("m2t")
&& !extension.equals("mpg")
&& !extension.equals("evo")
&& !extension.equals("mpeg")
&& !extension.equals("vob")
&& !extension.equals("m2v")
&& !extension.equals("mts")
&& !extension.equals("mov");
}
@Override
public int purpose() {
return VIDEO_SIMPLEFILE_PLAYER;
}
@Override
public String id() {
return ID;
}
@Override
public boolean isTimeSeekable() {
return true;
}
@Override
public String[] args() {
return null;
}
@Override
public String executable() {
return configuration.getTsmuxerPath();
}
@Override
public ProcessWrapper launchTranscode(
DLNAResource dlna,
DLNAMediaInfo media,
OutputParams params
) throws IOException {
// Use device-specific pms conf
PmsConfiguration prev = configuration;
configuration = (DeviceConfiguration) params.mediaRenderer;
final String filename = dlna.getSystemName();
setAudioAndSubs(filename, media, params);
PipeIPCProcess ffVideoPipe;
ProcessWrapperImpl ffVideo;
PipeIPCProcess ffAudioPipe[] = null;
ProcessWrapperImpl ffAudio[] = null;
String fps = media.getValidFps(false);
int width = media.getWidth();
int height = media.getHeight();
if (width < 320 || height < 240) {
width = -1;
height = -1;
}
String videoType = "V_MPEG4/ISO/AVC";
if (media.getCodecV() != null && media.getCodecV().startsWith("mpeg2")) {
videoType = "V_MPEG-2";
}
boolean aacTranscode = false;
String[] ffmpegCommands;
if (this instanceof TsMuxeRAudio && media.getFirstAudioTrack() != null) {
ffVideoPipe = new PipeIPCProcess(System.currentTimeMillis() + "fakevideo", System.currentTimeMillis() + "videoout", false, true);
String timeEndValue1 = "-t";
String timeEndValue2 = "" + params.timeend;
if (params.timeend < 1) {
timeEndValue1 = "-y";
timeEndValue2 = "-y";
}
ffmpegCommands = new String[] {
configuration.getFfmpegPath(),
timeEndValue1, timeEndValue2,
"-loop", "1",
"-i", "DummyInput.jpg",
"-f", "h264",
"-c:v", "libx264",
"-level", "31",
"-tune", "zerolatency",
"-pix_fmt", "yuv420p",
"-an",
"-y",
ffVideoPipe.getInputPipe()
};
videoType = "V_MPEG4/ISO/AVC";
OutputParams ffparams = new OutputParams(configuration);
ffparams.maxBufferSize = 1;
ffVideo = new ProcessWrapperImpl(ffmpegCommands, ffparams);
if (
filename.toLowerCase().endsWith(".flac") &&
media.getFirstAudioTrack().getBitsperSample() >= 24 &&
media.getFirstAudioTrack().getSampleRate() % 48000 == 0
) {
ffAudioPipe = new PipeIPCProcess[1];
ffAudioPipe[0] = new PipeIPCProcess(System.currentTimeMillis() + "flacaudio", System.currentTimeMillis() + "audioout", false, true);
String[] flacCmd = new String[] {
configuration.getFlacPath(),
"--output-name=" + ffAudioPipe[0].getInputPipe(),
"-d",
"-f",
"-F",
filename
};
ffparams = new OutputParams(configuration);
ffparams.maxBufferSize = 1;
ffAudio = new ProcessWrapperImpl[1];
ffAudio[0] = new ProcessWrapperImpl(flacCmd, ffparams);
} else {
ffAudioPipe = new PipeIPCProcess[1];
ffAudioPipe[0] = new PipeIPCProcess(System.currentTimeMillis() + "mlpaudio", System.currentTimeMillis() + "audioout", false, true);
String depth = "pcm_s16le";
String rate = "48000";
if (media.getFirstAudioTrack().getBitsperSample() >= 24) {
depth = "pcm_s24le";
}
if (media.getFirstAudioTrack().getSampleRate() > 48000) {
rate = "" + media.getFirstAudioTrack().getSampleRate();
}
String[] flacCmd = new String[] {
configuration.getFfmpegPath(),
"-i", filename,
"-ar", rate,
"-f", "wav",
"-acodec", depth,
"-y",
ffAudioPipe[0].getInputPipe()
};
ffparams = new OutputParams(configuration);
ffparams.maxBufferSize = 1;
ffAudio = new ProcessWrapperImpl[1];
ffAudio[0] = new ProcessWrapperImpl(flacCmd, ffparams);
}
} else {
params.waitbeforestart = 5000;
params.manageFastStart();
ffVideoPipe = new PipeIPCProcess(System.currentTimeMillis() + "ffmpegvideo", System.currentTimeMillis() + "videoout", false, true);
ffmpegCommands = new String[] {
configuration.getFfmpegPath(),
"-ss", params.timeseek > 0 ? "" + params.timeseek : "0",
"-i", filename,
"-c", "copy",
"-f", "rawvideo",
"-y",
ffVideoPipe.getInputPipe()
};
InputFile newInput = new InputFile();
newInput.setFilename(filename);
newInput.setPush(params.stdin);
/**
* Note: This logic is weird; on one hand we check if the renderer requires videos to be Level 4.1 or below, but then
* the other function allows the video to exceed those limits.
* In reality this won't cause problems since renderers typically don't support above 4.1 anyway - nor are many
* videos encoded higher than that either - but it's worth acknowledging the logic discrepancy.
*/
if (!media.isVideoWithinH264LevelLimits(newInput, params.mediaRenderer) && params.mediaRenderer.isH264Level41Limited()) {
LOGGER.info("The video will not play or will show a black screen");
}
if (media.getH264AnnexB() != null && media.getH264AnnexB().length > 0) {
StreamModifier sm = new StreamModifier();
sm.setHeader(media.getH264AnnexB());
sm.setH264AnnexB(true);
ffVideoPipe.setModifier(sm);
}
OutputParams ffparams = new OutputParams(configuration);
ffparams.maxBufferSize = 1;
ffparams.stdin = params.stdin;
ffVideo = new ProcessWrapperImpl(ffmpegCommands, ffparams);
int numAudioTracks = 1;
if (media.getAudioTracksList() != null && media.getAudioTracksList().size() > 1 && configuration.isMuxAllAudioTracks()) {
numAudioTracks = media.getAudioTracksList().size();
}
boolean singleMediaAudio = media.getAudioTracksList().size() <= 1;
if (params.aid != null) {
boolean ac3Remux;
boolean dtsRemux;
boolean encodedAudioPassthrough;
boolean pcm;
if (numAudioTracks <= 1) {
ffAudioPipe = new PipeIPCProcess[numAudioTracks];
ffAudioPipe[0] = new PipeIPCProcess(System.currentTimeMillis() + "ffmpegaudio01", System.currentTimeMillis() + "audioout", false, true);
encodedAudioPassthrough = configuration.isEncodedAudioPassthrough() && params.aid.isNonPCMEncodedAudio() && params.mediaRenderer.isWrapEncodedAudioIntoPCM();
ac3Remux = params.aid.isAC3() && configuration.isAudioRemuxAC3() && !encodedAudioPassthrough && !params.mediaRenderer.isTranscodeToAAC();
dtsRemux = configuration.isAudioEmbedDtsInPcm() && params.aid.isDTS() && params.mediaRenderer.isDTSPlayable() && !encodedAudioPassthrough;
pcm = configuration.isAudioUsePCM() &&
media.isValidForLPCMTranscoding() &&
(
params.aid.isLossless() ||
(params.aid.isDTS() && params.aid.getAudioProperties().getNumberOfChannels() <= 6) ||
params.aid.isTrueHD() ||
(
!configuration.isMencoderUsePcmForHQAudioOnly() &&
(
params.aid.isAC3() ||
params.aid.isMP3() ||
params.aid.isAAC() ||
params.aid.isVorbis() ||
// params.aid.isWMA() ||
params.aid.isMpegAudio()
)
)
) && params.mediaRenderer.isLPCMPlayable();
int channels;
if (ac3Remux) {
channels = params.aid.getAudioProperties().getNumberOfChannels(); // AC-3 remux
} else if (dtsRemux || encodedAudioPassthrough) {
channels = 2;
} else if (pcm) {
channels = params.aid.getAudioProperties().getNumberOfChannels();
} else {
channels = configuration.getAudioChannelCount(); // 5.1 max for AC-3 encoding
}
if (!ac3Remux && (dtsRemux || pcm || encodedAudioPassthrough)) {
// DTS remux or LPCM
StreamModifier sm = new StreamModifier();
sm.setPcm(pcm);
sm.setDtsEmbed(dtsRemux);
sm.setEncodedAudioPassthrough(encodedAudioPassthrough);
sm.setNbChannels(channels);
sm.setSampleFrequency(params.aid.getSampleRate() < 48000 ? 48000 : params.aid.getSampleRate());
sm.setBitsPerSample(16);
ffmpegCommands = new String[] {
configuration.getFfmpegPath(),
"-ss", params.timeseek > 0 ? "" + params.timeseek : "0",
"-i", filename,
"-ac", "" + sm.getNbChannels(),
"-f", "ac3",
"-c:a", sm.isDtsEmbed() || sm.isEncodedAudioPassthrough() ? "copy" : "pcm",
"-y",
ffAudioPipe[0].getInputPipe()
};
// Use PCM trick when media renderer does not support DTS in MPEG
if (!params.mediaRenderer.isMuxDTSToMpeg()) {
ffAudioPipe[0].setModifier(sm);
}
} else if (!ac3Remux && params.mediaRenderer.isTranscodeToAAC()) {
// AAC audio
ffmpegCommands = new String[] {
configuration.getFfmpegPath(),
"-ss", params.timeseek > 0 ? "" + params.timeseek : "0",
"-i", filename,
"-ac", "" + channels,
"-f", "adts",
"-c:a", "aac",
"-strict", "experimental",
"-ab", Math.min(configuration.getAudioBitrate(), 320) + "k",
"-y",
ffAudioPipe[0].getInputPipe()
};
aacTranscode = true;
} else {
// AC-3 audio
ffmpegCommands = new String[] {
configuration.getFfmpegPath(),
"-ss", params.timeseek > 0 ? "" + params.timeseek : "0",
"-i", filename,
"-ac", "" + channels,
"-f", "ac3",
"-c:a", (ac3Remux) ? "copy" : "ac3",
"-ab", String.valueOf(CodecUtil.getAC3Bitrate(configuration, params.aid)) + "k",
"-y",
ffAudioPipe[0].getInputPipe()
};
}
ffparams = new OutputParams(configuration);
ffparams.maxBufferSize = 1;
ffparams.stdin = params.stdin;
ffAudio = new ProcessWrapperImpl[numAudioTracks];
ffAudio[0] = new ProcessWrapperImpl(ffmpegCommands, ffparams);
} else {
ffAudioPipe = new PipeIPCProcess[numAudioTracks];
ffAudio = new ProcessWrapperImpl[numAudioTracks];
for (int i = 0; i < media.getAudioTracksList().size(); i++) {
DLNAMediaAudio audio = media.getAudioTracksList().get(i);
ffAudioPipe[i] = new PipeIPCProcess(System.currentTimeMillis() + "ffmpeg" + i, System.currentTimeMillis() + "audioout" + i, false, true);
encodedAudioPassthrough = configuration.isEncodedAudioPassthrough() && params.aid.isNonPCMEncodedAudio() && params.mediaRenderer.isWrapEncodedAudioIntoPCM();
ac3Remux = audio.isAC3() && configuration.isAudioRemuxAC3() && !encodedAudioPassthrough && !params.mediaRenderer.isTranscodeToAAC();
dtsRemux = configuration.isAudioEmbedDtsInPcm() && audio.isDTS() && params.mediaRenderer.isDTSPlayable() && !encodedAudioPassthrough;
pcm = configuration.isAudioUsePCM() &&
media.isValidForLPCMTranscoding() &&
(
audio.isLossless() ||
(audio.isDTS() && audio.getAudioProperties().getNumberOfChannels() <= 6) ||
audio.isTrueHD() ||
(
!configuration.isMencoderUsePcmForHQAudioOnly() &&
(
audio.isAC3() ||
audio.isMP3() ||
audio.isAAC() ||
audio.isVorbis() ||
// audio.isWMA() ||
audio.isMpegAudio()
)
)
) && params.mediaRenderer.isLPCMPlayable();
int channels;
if (ac3Remux) {
channels = audio.getAudioProperties().getNumberOfChannels(); // AC-3 remux
} else if (dtsRemux || encodedAudioPassthrough) {
channels = 2;
} else if (pcm) {
channels = audio.getAudioProperties().getNumberOfChannels();
} else {
channels = configuration.getAudioChannelCount(); // 5.1 max for AC-3 encoding
}
if (!ac3Remux && (dtsRemux || pcm || encodedAudioPassthrough)) {
// DTS remux or LPCM
StreamModifier sm = new StreamModifier();
sm.setPcm(pcm);
sm.setDtsEmbed(dtsRemux);
sm.setEncodedAudioPassthrough(encodedAudioPassthrough);
sm.setNbChannels(channels);
sm.setSampleFrequency(audio.getSampleRate() < 48000 ? 48000 : audio.getSampleRate());
sm.setBitsPerSample(16);
if (!params.mediaRenderer.isMuxDTSToMpeg()) {
ffAudioPipe[i].setModifier(sm);
}
ffmpegCommands = new String[] {
configuration.getFfmpegPath(),
"-ss", params.timeseek > 0 ? "" + params.timeseek : "0",
"-i", filename,
"-ac", "" + sm.getNbChannels(),
"-f", "ac3",
singleMediaAudio ? "-y" : "-map", singleMediaAudio ? "-y" : ("0:a:" + (media.getAudioTracksList().indexOf(audio))),
"-c:a", sm.isDtsEmbed() || sm.isEncodedAudioPassthrough() ? "copy" : "pcm",
"-y",
ffAudioPipe[i].getInputPipe()
};
} else if (!ac3Remux && params.mediaRenderer.isTranscodeToAAC()) {
// AAC audio
ffmpegCommands = new String[] {
configuration.getFfmpegPath(),
"-ss", params.timeseek > 0 ? "" + params.timeseek : "0",
"-i", filename,
"-ac", "" + channels,
"-f", "adts",
singleMediaAudio ? "-y" : "-map", singleMediaAudio ? "-y" : ("0:a:" + (media.getAudioTracksList().indexOf(audio))),
"-c:a", "aac",
"-strict", "experimental",
"-ab", Math.min(configuration.getAudioBitrate(), 320) + "k",
"-y",
ffAudioPipe[i].getInputPipe()
};
aacTranscode = true;
} else {
// AC-3 remux or encoding
ffmpegCommands = new String[] {
configuration.getFfmpegPath(),
"-ss", params.timeseek > 0 ? "" + params.timeseek : "0",
"-i", filename,
"-ac", "" + channels,
"-f", "ac3",
singleMediaAudio ? "-y" : "-map", singleMediaAudio ? "-y" : ("0:a:" + (media.getAudioTracksList().indexOf(audio))),
"-c:a", (ac3Remux) ? "copy" : "ac3",
"-ab", String.valueOf(CodecUtil.getAC3Bitrate(configuration, audio)) + "k",
"-y",
ffAudioPipe[i].getInputPipe()
};
}
ffparams = new OutputParams(configuration);
ffparams.maxBufferSize = 1;
ffparams.stdin = params.stdin;
ffAudio[i] = new ProcessWrapperImpl(ffmpegCommands, ffparams);
}
}
}
}
File f = new File(configuration.getTempFolder(), "pms-tsmuxer.meta");
params.log = false;
try (PrintWriter pw = new PrintWriter(f)) {
pw.print("MUXOPT --no-pcr-on-video-pid");
pw.print(" --new-audio-pes");
pw.print(" --no-asyncio");
pw.print(" --vbr");
pw.println(" --vbv-len=500");
String sei = "insertSEI";
if (
params.mediaRenderer.isPS3() &&
media.isWebDl(filename, params)
) {
sei = "forceSEI";
}
String videoparams = "level=4.1, " + sei + ", contSPS, track=1";
if (this instanceof TsMuxeRAudio) {
videoparams = "track=224";
}
if (configuration.isFix25FPSAvMismatch()) {
fps = "25";
}
pw.println(videoType + ", \"" + ffVideoPipe.getOutputPipe() + "\", " + (fps != null ? ("fps=" + fps + ", ") : "") + (width != -1 ? ("video-width=" + width + ", ") : "") + (height != -1 ? ("video-height=" + height + ", ") : "") + videoparams);
if (ffAudioPipe != null && ffAudioPipe.length == 1) {
String timeshift = "";
boolean ac3Remux;
boolean dtsRemux;
boolean encodedAudioPassthrough;
boolean pcm;
encodedAudioPassthrough = configuration.isEncodedAudioPassthrough() && params.aid.isNonPCMEncodedAudio() && params.mediaRenderer.isWrapEncodedAudioIntoPCM();
ac3Remux = params.aid.isAC3() && configuration.isAudioRemuxAC3() && !encodedAudioPassthrough && !params.mediaRenderer.isTranscodeToAAC();
dtsRemux = configuration.isAudioEmbedDtsInPcm() && params.aid.isDTS() && params.mediaRenderer.isDTSPlayable() && !encodedAudioPassthrough;
pcm = configuration.isAudioUsePCM() &&
media.isValidForLPCMTranscoding() &&
(
params.aid.isLossless() ||
(params.aid.isDTS() && params.aid.getAudioProperties().getNumberOfChannels() <= 6) ||
params.aid.isTrueHD() ||
(
!configuration.isMencoderUsePcmForHQAudioOnly() &&
(
params.aid.isAC3() ||
params.aid.isMP3() ||
params.aid.isAAC() ||
params.aid.isVorbis() ||
// params.aid.isWMA() ||
params.aid.isMpegAudio()
)
)
) && params.mediaRenderer.isLPCMPlayable();
String type = "A_AC3";
if (ac3Remux) {
// AC-3 remux takes priority
type = "A_AC3";
} else if (aacTranscode) {
type = "A_AAC";
} else {
if (pcm || this instanceof TsMuxeRAudio) {
type = "A_LPCM";
}
if (encodedAudioPassthrough || this instanceof TsMuxeRAudio) {
type = "A_LPCM";
}
if (dtsRemux || this instanceof TsMuxeRAudio) {
type = "A_LPCM";
if (params.mediaRenderer.isMuxDTSToMpeg()) {
type = "A_DTS";
}
}
}
if (params.aid != null && params.aid.getAudioProperties().getAudioDelay() != 0 && params.timeseek == 0) {
timeshift = "timeshift=" + params.aid.getAudioProperties().getAudioDelay() + "ms, ";
}
pw.println(type + ", \"" + ffAudioPipe[0].getOutputPipe() + "\", " + timeshift + "track=2");
} else if (ffAudioPipe != null) {
for (int i = 0; i < media.getAudioTracksList().size(); i++) {
DLNAMediaAudio lang = media.getAudioTracksList().get(i);
String timeshift = "";
boolean ac3Remux;
boolean dtsRemux;
boolean encodedAudioPassthrough;
boolean pcm;
encodedAudioPassthrough = configuration.isEncodedAudioPassthrough() && params.aid.isNonPCMEncodedAudio() && params.mediaRenderer.isWrapEncodedAudioIntoPCM();
ac3Remux = lang.isAC3() && configuration.isAudioRemuxAC3() && !encodedAudioPassthrough;
dtsRemux = configuration.isAudioEmbedDtsInPcm() && lang.isDTS() && params.mediaRenderer.isDTSPlayable() && !encodedAudioPassthrough;
pcm = configuration.isAudioUsePCM() &&
media.isValidForLPCMTranscoding() &&
(
lang.isLossless() ||
(lang.isDTS() && lang.getAudioProperties().getNumberOfChannels() <= 6) ||
lang.isTrueHD() ||
(
!configuration.isMencoderUsePcmForHQAudioOnly() &&
(
params.aid.isAC3() ||
params.aid.isMP3() ||
params.aid.isAAC() ||
params.aid.isVorbis() ||
// params.aid.isWMA() ||
params.aid.isMpegAudio()
)
)
) && params.mediaRenderer.isLPCMPlayable();
String type = "A_AC3";
if (ac3Remux) {
// AC-3 remux takes priority
type = "A_AC3";
} else {
if (pcm) {
type = "A_LPCM";
}
if (encodedAudioPassthrough) {
type = "A_LPCM";
}
if (dtsRemux) {
type = "A_LPCM";
if (params.mediaRenderer.isMuxDTSToMpeg()) {
type = "A_DTS";
}
}
}
if (lang.getAudioProperties().getAudioDelay() != 0 && params.timeseek == 0) {
timeshift = "timeshift=" + lang.getAudioProperties().getAudioDelay() + "ms, ";
}
pw.println(type + ", \"" + ffAudioPipe[i].getOutputPipe() + "\", " + timeshift + "track=" + (2 + i));
}
}
}
PipeProcess tsPipe = new PipeProcess(System.currentTimeMillis() + "tsmuxerout.ts");
/**
* Use the newer version of tsMuxeR on PS3 since other renderers
* like Panasonic TVs don't always recognize the new output
*/
String executable = executable();
if (params.mediaRenderer.isPS3()) {
executable = configuration.getTsmuxerNewPath();
}
String[] cmdArray = new String[]{
executable,
f.getAbsolutePath(),
tsPipe.getInputPipe()
};
cmdArray = finalizeTranscoderArgs(
filename,
dlna,
media,
params,
cmdArray
);
ProcessWrapperImpl p = new ProcessWrapperImpl(cmdArray, params);
params.maxBufferSize = 100;
params.input_pipes[0] = tsPipe;
params.stdin = null;
ProcessWrapper pipe_process = tsPipe.getPipeProcess();
p.attachProcess(pipe_process);
pipe_process.runInNewThread();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
}
tsPipe.deleteLater();
ProcessWrapper ff_pipe_process = ffVideoPipe.getPipeProcess();
p.attachProcess(ff_pipe_process);
ff_pipe_process.runInNewThread();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
}
ffVideoPipe.deleteLater();
p.attachProcess(ffVideo);
ffVideo.runInNewThread();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
}
if (ffAudioPipe != null && params.aid != null) {
for (int i = 0; i < ffAudioPipe.length; i++) {
ff_pipe_process = ffAudioPipe[i].getPipeProcess();
p.attachProcess(ff_pipe_process);
ff_pipe_process.runInNewThread();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
}
ffAudioPipe[i].deleteLater();
p.attachProcess(ffAudio[i]);
ffAudio[i].runInNewThread();
}
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
p.runInNewThread();
configuration = prev;
return p;
}
@Override
public String mimeType() {
return "video/mpeg";
}
@Override
public String name() {
return "tsMuxeR";
}
@Override
public int type() {
return Format.VIDEO;
}
private JCheckBox tsmuxerforcefps;
private JCheckBox muxallaudiotracks;
@Override
public JComponent config() {
// Apply the orientation for the locale
ComponentOrientation orientation = ComponentOrientation.getOrientation(PMS.getLocale());
String colSpec = FormLayoutUtil.getColSpec(COL_SPEC, orientation);
FormLayout layout = new FormLayout(colSpec, ROW_SPEC);
PanelBuilder builder = new PanelBuilder(layout);
builder.border(Borders.EMPTY);
builder.opaque(false);
CellConstraints cc = new CellConstraints();
JComponent cmp = builder.addSeparator(Messages.getString("NetworkTab.5"), FormLayoutUtil.flip(cc.xyw(2, 1, 1), colSpec, orientation));
cmp = (JComponent) cmp.getComponent(0);
cmp.setFont(cmp.getFont().deriveFont(Font.BOLD));
tsmuxerforcefps = new JCheckBox(Messages.getString("TsMuxeRVideo.2"), configuration.isTsmuxerForceFps());
tsmuxerforcefps.setContentAreaFilled(false);
tsmuxerforcefps.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
configuration.setTsmuxerForceFps(e.getStateChange() == ItemEvent.SELECTED);
}
});
builder.add(GuiUtil.getPreferredSizeComponent(tsmuxerforcefps), FormLayoutUtil.flip(cc.xy(2, 3), colSpec, orientation));
muxallaudiotracks = new JCheckBox(Messages.getString("TsMuxeRVideo.19"), configuration.isMuxAllAudioTracks());
muxallaudiotracks.setContentAreaFilled(false);
muxallaudiotracks.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
configuration.setMuxAllAudioTracks(e.getStateChange() == ItemEvent.SELECTED);
}
});
builder.add(GuiUtil.getPreferredSizeComponent(muxallaudiotracks), FormLayoutUtil.flip(cc.xy(2, 5), colSpec, orientation));
JPanel panel = builder.getPanel();
// Apply the orientation to the panel and all components in it
panel.applyComponentOrientation(orientation);
return panel;
}
@Override
public boolean isInternalSubtitlesSupported() {
return false;
}
@Override
public boolean isExternalSubtitlesSupported() {
return false;
}
@Override
public boolean isPlayerCompatible(RendererConfiguration mediaRenderer) {
return mediaRenderer != null && mediaRenderer.isMuxH264MpegTS();
}
/**
* {@inheritDoc}
*/
@Override
public boolean isCompatible(DLNAResource resource) {
DLNAMediaSubtitle subtitle = resource.getMediaSubtitle();
// Check whether the subtitle actually has a language defined,
// uninitialized DLNAMediaSubtitle objects have a null language.
if (subtitle != null && subtitle.getLang() != null) {
// The resource needs a subtitle, but PMS does not support subtitles for tsMuxeR.
return false;
}
try {
String audioTrackName = resource.getMediaAudio().toString();
String defaultAudioTrackName = resource.getMedia().getAudioTracksList().get(0).toString();
if (!audioTrackName.equals(defaultAudioTrackName)) {
// PMS only supports playback of the default audio track for tsMuxeR
return false;
}
} catch (NullPointerException e) {
LOGGER.trace("tsMuxeR cannot determine compatibility based on audio track for " + resource.getSystemName());
} catch (IndexOutOfBoundsException e) {
LOGGER.trace("tsMuxeR cannot determine compatibility based on default audio track for " + resource.getSystemName());
}
if (
PlayerUtil.isVideo(resource, Format.Identifier.MKV) ||
PlayerUtil.isVideo(resource, Format.Identifier.MPG)
) {
return true;
}
return false;
}
}
| bigretromike/UniversalMediaServer | src/main/java/net/pms/encoders/TsMuxeRVideo.java | Java | gpl-2.0 | 27,024 |
/*
* Copyright (c) 2004, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 4997799
* @summary Basic test of ThreadMXBean.getThreadUserTime and
* getCurrentThreadUserTime.
* @author Mandy Chung
* @modules java.management
*/
import java.lang.management.*;
public class ThreadUserTime {
private static ThreadMXBean mbean = ManagementFactory.getThreadMXBean();
private static boolean testFailed = false;
private static boolean done = false;
private static Object obj = new Object();
private static final int NUM_THREADS = 10;
private static Thread[] threads = new Thread[NUM_THREADS];
private static long[] times = new long[NUM_THREADS];
// careful about this value
private static final int DELTA = 100;
public static void main(String[] argv)
throws Exception {
if (!mbean.isCurrentThreadCpuTimeSupported()) {
return;
}
// disable user time
if (mbean.isThreadCpuTimeEnabled()) {
mbean.setThreadCpuTimeEnabled(false);
}
Thread curThread = Thread.currentThread();
long t = mbean.getCurrentThreadUserTime();
if (t != -1) {
throw new RuntimeException("Invalid CurrenThreadUserTime returned = " +
t + " expected = -1");
}
if (mbean.isThreadCpuTimeSupported()) {
long t1 = mbean.getThreadUserTime(curThread.getId());
if (t1 != -1) {
throw new RuntimeException("Invalid ThreadUserTime returned = " +
t1 + " expected = -1");
}
}
// Enable CPU Time measurement
if (!mbean.isThreadCpuTimeEnabled()) {
mbean.setThreadCpuTimeEnabled(true);
}
if (!mbean.isThreadCpuTimeEnabled()) {
throw new RuntimeException("ThreadUserTime is expected to be enabled");
}
long time = mbean.getCurrentThreadUserTime();
if (time < 0) {
throw new RuntimeException("Invalid user time returned = " + time);
}
if (!mbean.isThreadCpuTimeSupported()) {
return;
}
// Expected to be time1 >= time
long time1 = mbean.getThreadUserTime(curThread.getId());
if (time1 < time) {
throw new RuntimeException("User time " + time1 +
" expected >= " + time);
}
System.out.println(curThread.getName() +
" Current Thread User Time = " + time +
" user time = " + time1);
for (int i = 0; i < NUM_THREADS; i++) {
threads[i] = new MyThread("MyThread-" + i);
threads[i].start();
}
waitUntilThreadBlocked();
for (int i = 0; i < NUM_THREADS; i++) {
times[i] = mbean.getThreadUserTime(threads[i].getId());
}
goSleep(200);
for (int i = 0; i < NUM_THREADS; i++) {
long newTime = mbean.getThreadUserTime(threads[i].getId());
if (times[i] > newTime) {
throw new RuntimeException("TEST FAILED: " +
threads[i].getName() +
" previous user user time = " + times[i] +
" > current user user time = " + newTime);
}
if ((times[i] + DELTA) < newTime) {
throw new RuntimeException("TEST FAILED: " +
threads[i].getName() +
" user time = " + newTime +
" previous user time " + times[i] +
" out of expected range");
}
System.out.println(threads[i].getName() +
" Previous User Time = " + times[i] +
" Current User time = " + newTime);
}
synchronized (obj) {
done = true;
obj.notifyAll();
}
for (int i = 0; i < NUM_THREADS; i++) {
try {
threads[i].join();
} catch (InterruptedException e) {
System.out.println("Unexpected exception is thrown.");
e.printStackTrace(System.out);
testFailed = true;
break;
}
}
if (testFailed) {
throw new RuntimeException("TEST FAILED");
}
System.out.println("Test passed");
}
private static void goSleep(long ms) throws Exception {
try {
Thread.sleep(ms);
} catch (InterruptedException e) {
System.out.println("Unexpected exception is thrown.");
throw e;
}
}
private static void waitUntilThreadBlocked()
throws Exception {
int count = 0;
while (count != NUM_THREADS) {
goSleep(100);
count = 0;
for (int i = 0; i < NUM_THREADS; i++) {
ThreadInfo info = mbean.getThreadInfo(threads[i].getId());
if (info.getThreadState() == Thread.State.WAITING) {
count++;
}
}
}
}
static class MyThread extends Thread {
public MyThread(String name) {
super(name);
}
public void run() {
double sum = 0;
for (int i = 0; i < 5000; i++) {
double r = Math.random();
double x = Math.pow(3, r);
sum += x - r;
}
synchronized (obj) {
while (!done) {
try {
obj.wait();
} catch (InterruptedException e) {
System.out.println("Unexpected exception is thrown.");
e.printStackTrace(System.out);
testFailed = true;
break;
}
}
}
sum = 0;
for (int i = 0; i < 5000; i++) {
double r = Math.random();
double x = Math.pow(3, r);
sum += x - r;
}
long time1 = mbean.getCurrentThreadCpuTime();
long utime1 = mbean.getCurrentThreadUserTime();
long time2 = mbean.getThreadCpuTime(getId());
long utime2 = mbean.getThreadUserTime(getId());
System.out.println(getName() + ":");
System.out.println("CurrentThreadUserTime = " + utime1 +
" ThreadUserTime = " + utime2);
System.out.println("CurrentThreadCpuTime = " + time1 +
" ThreadCpuTime = " + time2);
if (time1 > time2) {
throw new RuntimeException("TEST FAILED: " + getName() +
" CurrentThreadCpuTime = " + time1 +
" > ThreadCpuTime = " + time2);
}
if (utime1 > utime2) {
throw new RuntimeException("TEST FAILED: " + getName() +
" CurrentThreadUserTime = " + utime1 +
" > ThreadUserTime = " + utime2);
}
}
}
}
| FauxFaux/jdk9-jdk | test/java/lang/management/ThreadMXBean/ThreadUserTime.java | Java | gpl-2.0 | 8,036 |
/*****************************************************************************
* SearchFragment.java
*****************************************************************************
* Copyright © 2014-2015 VLC authors, VideoLAN and VideoLabs
* Author: Geoffrey Métais
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
package org.videolan.vlc.gui.tv;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v17.leanback.widget.ArrayObjectAdapter;
import android.support.v17.leanback.widget.HeaderItem;
import android.support.v17.leanback.widget.ListRow;
import android.support.v17.leanback.widget.ListRowPresenter;
import android.support.v17.leanback.widget.ObjectAdapter;
import android.support.v17.leanback.widget.OnItemViewClickedListener;
import android.support.v17.leanback.widget.Presenter;
import android.support.v17.leanback.widget.Row;
import android.support.v17.leanback.widget.RowPresenter;
import android.text.TextUtils;
import org.videolan.vlc.R;
import org.videolan.vlc.VLCApplication;
import org.videolan.vlc.media.MediaLibrary;
import org.videolan.vlc.media.MediaWrapper;
import java.util.ArrayList;
public class SearchFragment extends android.support.v17.leanback.app.SearchFragment
implements android.support.v17.leanback.app.SearchFragment.SearchResultProvider {
private static final String TAG = "SearchFragment";
private ArrayObjectAdapter mRowsAdapter;
private Handler mHandler = new Handler();
private SearchRunnable mDelayedLoad;
protected Activity mActivity;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mRowsAdapter = new ArrayObjectAdapter(new ListRowPresenter());
setSearchResultProvider(this);
setOnItemViewClickedListener(getDefaultItemClickedListener());
mDelayedLoad = new SearchRunnable();
mActivity = getActivity();
}
@Override
public ObjectAdapter getResultsAdapter() {
return mRowsAdapter;
}
private void queryByWords(String words) {
mRowsAdapter.clear();
if (!TextUtils.isEmpty(words) && words.length() > 2) {
mDelayedLoad.setSearchQuery(words);
mDelayedLoad.setSearchType(MediaWrapper.TYPE_ALL);
VLCApplication.runBackground(mDelayedLoad);
}
}
@Override
public boolean onQueryTextChange(String newQuery) {
queryByWords(newQuery);
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
queryByWords(query);
return true;
}
private void loadRows(String query, int type) {
ArrayList<MediaWrapper> mediaList = MediaLibrary.getInstance().searchMedia(query);
final ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(new CardPresenter(mActivity));
listRowAdapter.addAll(0, mediaList);
mHandler.post(new Runnable() {
@Override
public void run() {
HeaderItem header = new HeaderItem(0, getResources().getString(R.string.search_results));
mRowsAdapter.add(new ListRow(header, listRowAdapter));
}
});
}
protected OnItemViewClickedListener getDefaultItemClickedListener() {
return new OnItemViewClickedListener() {
@Override
public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item, RowPresenter.ViewHolder rowViewHolder, Row row) {
if (item instanceof MediaWrapper) {
TvUtil.openMedia(mActivity, (MediaWrapper) item, row);
}
}
};
}
private class SearchRunnable implements Runnable {
private volatile String searchQuery;
private volatile int searchType;
public SearchRunnable() {}
public void run() {
loadRows(searchQuery, searchType);
}
public void setSearchQuery(String value) {
this.searchQuery = value;
}
public void setSearchType(int value) {
this.searchType = value;
}
}
}
| hanhailong/VCL-Official | vlc-android/src/org/videolan/vlc/gui/tv/SearchFragment.java | Java | gpl-2.0 | 4,897 |
/*
* Copyright (c) 2016-2019 Projekt Substratum
* This file is part of Substratum.
*
* SPDX-License-Identifier: GPL-3.0-Or-Later
*/
package projekt.substratum.common;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import projekt.substratum.Substratum;
import projekt.substratum.services.crash.AppCrashReceiver;
import projekt.substratum.services.packages.OverlayFound;
import projekt.substratum.services.packages.OverlayUpdater;
import projekt.substratum.services.packages.PackageModificationDetector;
import projekt.substratum.services.profiles.ScheduledProfileReceiver;
import projekt.substratum.services.system.InterfacerAuthorizationReceiver;
import static projekt.substratum.common.Internal.ENCRYPTION_KEY_EXTRA;
import static projekt.substratum.common.Internal.IV_ENCRYPTION_KEY_EXTRA;
import static projekt.substratum.common.Internal.MAIN_ACTIVITY_RECEIVER;
import static projekt.substratum.common.Internal.OVERLAY_REFRESH;
import static projekt.substratum.common.Internal.THEME_FRAGMENT_REFRESH;
import static projekt.substratum.common.References.ACTIVITY_FINISHER;
import static projekt.substratum.common.References.APP_CRASHED;
import static projekt.substratum.common.References.INTERFACER_PACKAGE;
import static projekt.substratum.common.References.KEY_RETRIEVAL;
import static projekt.substratum.common.References.MANAGER_REFRESH;
import static projekt.substratum.common.References.PACKAGE_ADDED;
import static projekt.substratum.common.References.PACKAGE_FULLY_REMOVED;
import static projekt.substratum.common.References.SUBSTRATUM_LOG;
import static projekt.substratum.common.References.TEMPLATE_RECEIVE_KEYS;
import static projekt.substratum.common.References.scheduledProfileReceiver;
public class Broadcasts {
/**
* Send a localized key message for encryption to take place
*
* @param context Context
* @param encryptionKey Encryption key
* @param ivEncryptKey IV encryption key
*/
private static void sendLocalizedKeyMessage(Context context,
byte[] encryptionKey,
byte[] ivEncryptKey) {
Substratum.log("KeyRetrieval",
"The system has completed the handshake for keys retrieval " +
"and is now passing it to the activity...");
Intent intent = new Intent(KEY_RETRIEVAL);
intent.putExtra(ENCRYPTION_KEY_EXTRA, encryptionKey);
intent.putExtra(IV_ENCRYPTION_KEY_EXTRA, ivEncryptKey);
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
}
/**
* Close Substratum as a whole
*
* @param context Context
*/
public static void sendKillMessage(Context context) {
Substratum.log("SubstratumKiller",
"A crucial action has been conducted by the user and " +
"Substratum is now shutting down!");
Intent intent = new Intent(MAIN_ACTIVITY_RECEIVER);
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
}
/**
* A package was installed, refresh the ThemeFragment
*
* @param context Context
*/
public static void sendRefreshMessage(Context context) {
Substratum.log("ThemeFragmentRefresher",
"A theme has been modified, sending update signal to refresh the list!");
Intent intent = new Intent(THEME_FRAGMENT_REFRESH);
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
}
/**
* A package was installed, refresh the Overlays tab
*
* @param context Context
*/
public static void sendOverlayRefreshMessage(Context context) {
Substratum.log("OverlayRefresher",
"A theme has been modified, sending update signal to refresh the list!");
Intent intent = new Intent(OVERLAY_REFRESH);
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
}
/**
* Activity finisher when a theme was updated
*
* @param context Context
* @param packageName Package of theme to close
*/
public static void sendActivityFinisherMessage(Context context,
String packageName) {
Substratum.log("ThemeInstaller",
"A theme has been installed, sending update signal to app for further processing!");
Intent intent = new Intent(ACTIVITY_FINISHER);
intent.putExtra(Internal.THEME_PID, packageName);
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
}
/**
* A package was installed, refresh the ManagerFragment
*
* @param context Context
*/
public static void sendRefreshManagerMessage(Context context) {
Intent intent = new Intent(MANAGER_REFRESH);
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
}
/**
* Register the implicit intent broadcast receivers
*
* @param context Context
*/
public static void registerBroadcastReceivers(Context context) {
try {
IntentFilter intentPackageAdded = new IntentFilter(PACKAGE_ADDED);
intentPackageAdded.addDataScheme("package");
IntentFilter intentPackageFullyRemoved = new IntentFilter(PACKAGE_FULLY_REMOVED);
intentPackageFullyRemoved.addDataScheme("package");
if (Systems.checkOMS(context)) {
IntentFilter intentAppCrashed = new IntentFilter(APP_CRASHED);
context.getApplicationContext().registerReceiver(
new AppCrashReceiver(), intentAppCrashed);
context.getApplicationContext().registerReceiver(
new OverlayUpdater(), intentPackageAdded);
}
if (Systems.checkThemeInterfacer(context)) {
IntentFilter interfacerAuthorize = new IntentFilter(
INTERFACER_PACKAGE + ".CALLER_AUTHORIZED");
context.getApplicationContext().registerReceiver(
new InterfacerAuthorizationReceiver(), interfacerAuthorize);
}
context.getApplicationContext().registerReceiver(
new OverlayFound(), intentPackageAdded);
context.getApplicationContext().registerReceiver(
new PackageModificationDetector(), intentPackageAdded);
context.getApplicationContext().registerReceiver(
new PackageModificationDetector(), intentPackageFullyRemoved);
Substratum.log(SUBSTRATUM_LOG,
"Successfully registered broadcast receivers for Substratum functionality!");
} catch (Exception e) {
Log.e(SUBSTRATUM_LOG,
"Failed to register broadcast receivers for Substratum functionality...");
}
}
/**
* Register the profile screen off receiver
*
* @param context Context
*/
public static void registerProfileScreenOffReceiver(Context context) {
scheduledProfileReceiver = new ScheduledProfileReceiver();
context.registerReceiver(scheduledProfileReceiver,
new IntentFilter(Intent.ACTION_SCREEN_OFF));
}
/**
* Unload the profile screen off receiver
*
* @param context Context
*/
public static void unregisterProfileScreenOffReceiver(Context context) {
try {
context.unregisterReceiver(scheduledProfileReceiver);
} catch (Exception ignored) {
}
}
/**
* Start the key retrieval receiver to obtain the key from the theme
*
* @param context Context
*/
public static void startKeyRetrievalReceiver(Context context) {
try {
IntentFilter intentGetKeys = new IntentFilter(TEMPLATE_RECEIVE_KEYS);
context.getApplicationContext().registerReceiver(
new KeyRetriever(), intentGetKeys);
Substratum.log(SUBSTRATUM_LOG, "Successfully registered key retrieval receiver!");
} catch (Exception e) {
Log.e(SUBSTRATUM_LOG, "Failed to register key retrieval receiver...");
}
}
/**
* Key Retriever Receiver
*/
public static class KeyRetriever extends BroadcastReceiver {
@Override
public void onReceive(Context context,
Intent intent) {
sendLocalizedKeyMessage(
context,
intent.getByteArrayExtra(ENCRYPTION_KEY_EXTRA),
intent.getByteArrayExtra(IV_ENCRYPTION_KEY_EXTRA));
}
}
} | nicholaschum/substratum | app/src/main/java/projekt/substratum/common/Broadcasts.java | Java | gpl-3.0 | 8,846 |
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.rocketmq.client.hook;
import java.util.Map;
import com.alibaba.rocketmq.client.impl.CommunicationMode;
import com.alibaba.rocketmq.client.producer.SendResult;
import com.alibaba.rocketmq.common.message.Message;
import com.alibaba.rocketmq.common.message.MessageQueue;
public class SendMessageContext {
private String producerGroup;
private Message message;
private MessageQueue mq;
private String brokerAddr;
private String bornHost;
private CommunicationMode communicationMode;
private SendResult sendResult;
private Exception exception;
private Object mqTraceContext;
private Map<String, String> props;
public String getProducerGroup() {
return producerGroup;
}
public void setProducerGroup(String producerGroup) {
this.producerGroup = producerGroup;
}
public Message getMessage() {
return message;
}
public void setMessage(Message message) {
this.message = message;
}
public MessageQueue getMq() {
return mq;
}
public void setMq(MessageQueue mq) {
this.mq = mq;
}
public String getBrokerAddr() {
return brokerAddr;
}
public void setBrokerAddr(String brokerAddr) {
this.brokerAddr = brokerAddr;
}
public CommunicationMode getCommunicationMode() {
return communicationMode;
}
public void setCommunicationMode(CommunicationMode communicationMode) {
this.communicationMode = communicationMode;
}
public SendResult getSendResult() {
return sendResult;
}
public void setSendResult(SendResult sendResult) {
this.sendResult = sendResult;
}
public Exception getException() {
return exception;
}
public void setException(Exception exception) {
this.exception = exception;
}
public Object getMqTraceContext() {
return mqTraceContext;
}
public void setMqTraceContext(Object mqTraceContext) {
this.mqTraceContext = mqTraceContext;
}
public Map<String, String> getProps() {
return props;
}
public void setProps(Map<String, String> props) {
this.props = props;
}
public String getBornHost() {
return bornHost;
}
public void setBornHost(String bornHost) {
this.bornHost = bornHost;
}
}
| y123456yz/reading-and-annotate-rocketmq-3.4.6 | rocketmq-src/RocketMQ-3.4.6/rocketmq-client/src/main/java/com/alibaba/rocketmq/client/hook/SendMessageContext.java | Java | gpl-3.0 | 3,205 |
/* Copyright (c) 2001-2008, The HSQL Development Group
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of the HSQL Development Group nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.hsqldb.sample;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import org.hsqldb.jdbc.jdbcDataSource;
/**
* Title: Testdb
* Description: simple hello world db example of a
* standalone persistent db application
*
* every time it runs it adds four more rows to sample_table
* it does a query and prints the results to standard out
*
* Author: Karl Meissner karl@meissnersd.com
*/
public class Testdb {
Connection conn; //our connnection to the db - presist for life of program
// we dont want this garbage collected until we are done
public Testdb(String db_file_name_prefix) throws Exception { // note more general exception
// connect to the database. This will load the db files and start the
// database if it is not alread running.
// db_file_name_prefix is used to open or create files that hold the state
// of the db.
// It can contain directory names relative to the
// current working directory
jdbcDataSource dataSource = new jdbcDataSource();
dataSource.setDatabase("jdbc:hsqldb:" + db_file_name_prefix);
Connection c = dataSource.getConnection("sa", "");
}
public void shutdown() throws SQLException {
Statement st = conn.createStatement();
// db writes out to files and performs clean shuts down
// otherwise there will be an unclean shutdown
// when program ends
st.execute("SHUTDOWN");
conn.close(); // if there are no other open connection
}
//use for SQL command SELECT
public synchronized void query(String expression) throws SQLException {
Statement st = null;
ResultSet rs = null;
st = conn.createStatement(); // statement objects can be reused with
// repeated calls to execute but we
// choose to make a new one each time
rs = st.executeQuery(expression); // run the query
// do something with the result set.
dump(rs);
st.close(); // NOTE!! if you close a statement the associated ResultSet is
// closed too
// so you should copy the contents to some other object.
// the result set is invalidated also if you recycle an Statement
// and try to execute some other query before the result set has been
// completely examined.
}
//use for SQL commands CREATE, DROP, INSERT and UPDATE
public synchronized void update(String expression) throws SQLException {
Statement st = null;
st = conn.createStatement(); // statements
int i = st.executeUpdate(expression); // run the query
if (i == -1) {
System.out.println("db error : " + expression);
}
st.close();
} // void update()
public static void dump(ResultSet rs) throws SQLException {
// the order of the rows in a cursor
// are implementation dependent unless you use the SQL ORDER statement
ResultSetMetaData meta = rs.getMetaData();
int colmax = meta.getColumnCount();
int i;
Object o = null;
// the result set is a cursor into the data. You can only
// point to one row at a time
// assume we are pointing to BEFORE the first row
// rs.next() points to next row and returns true
// or false if there is no next row, which breaks the loop
for (; rs.next(); ) {
for (i = 0; i < colmax; ++i) {
o = rs.getObject(i + 1); // Is SQL the first column is indexed
// with 1 not 0
System.out.print(o.toString() + " ");
}
System.out.println(" ");
}
} //void dump( ResultSet rs )
public static void main(String[] args) {
Testdb db = null;
try {
db = new Testdb("db_file");
} catch (Exception ex1) {
ex1.printStackTrace(); // could not start db
return; // bye bye
}
try {
//make an empty table
//
// by declaring the id column IDENTITY, the db will automatically
// generate unique values for new rows- useful for row keys
db.update(
"CREATE TABLE sample_table ( id INTEGER IDENTITY, str_col VARCHAR(256), num_col INTEGER)");
} catch (SQLException ex2) {
//ignore
//ex2.printStackTrace(); // second time we run program
// should throw execption since table
// already there
//
// this will have no effect on the db
}
try {
// add some rows - will create duplicates if run more then once
// the id column is automatically generated
db.update(
"INSERT INTO sample_table(str_col,num_col) VALUES('Ford', 100)");
db.update(
"INSERT INTO sample_table(str_col,num_col) VALUES('Toyota', 200)");
db.update(
"INSERT INTO sample_table(str_col,num_col) VALUES('Honda', 300)");
db.update(
"INSERT INTO sample_table(str_col,num_col) VALUES('GM', 400)");
// do a query
db.query("SELECT * FROM sample_table WHERE num_col < 250");
// at end of program
db.shutdown();
} catch (SQLException ex3) {
ex3.printStackTrace();
}
} // main()
} // class Testdb
| ckaestne/LEADT | workspace/hsqldb/src/org/hsqldb/sample/Testdb.java | Java | gpl-3.0 | 7,388 |
/*
* Copyright (c) 2002-2008 LWJGL Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'LWJGL' nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.lwjgl.opengl;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.Canvas;
import org.lwjgl.LWJGLException;
/**
*
* @author elias_naur <elias_naur@users.sourceforge.net>
* @version $Revision: 3632 $
* $Id: MacOSXCanvasImplementation.java 3632 2011-09-03 18:52:45Z spasi $
*/
final class MacOSXCanvasImplementation implements AWTCanvasImplementation {
public PeerInfo createPeerInfo(Canvas component, PixelFormat pixel_format, ContextAttribs attribs) throws LWJGLException {
try {
return new MacOSXAWTGLCanvasPeerInfo(component, pixel_format, attribs, true);
} catch (LWJGLException e) {
return new MacOSXAWTGLCanvasPeerInfo(component, pixel_format, attribs, false);
}
}
/**
* Find a proper GraphicsConfiguration from the given GraphicsDevice and PixelFormat.
*
* @return The GraphicsConfiguration corresponding to a visual that matches the pixel format.
*/
public GraphicsConfiguration findConfiguration(GraphicsDevice device, PixelFormat pixel_format) throws LWJGLException {
/*
* It seems like the best way is to simply return null
*/
return null;
}
}
| kevinwang/minecarft | lwjgl-source-2.8.2/src/java/org/lwjgl/opengl/MacOSXCanvasImplementation.java | Java | gpl-3.0 | 2,780 |
/* Android IMSI-Catcher Detector | (c) AIMSICD Privacy Project
* -----------------------------------------------------------
* LICENSE: http://git.io/vki47 | TERMS: http://git.io/vki4o
* -----------------------------------------------------------
*/
package com.secupwn.aimsicd.ui.drawer;
import android.support.annotation.DrawableRes;
public interface NavDrawerItem {
int getId();
String getLabel();
void setLabel(String label);
void setIconId(@DrawableRes int icon);
int getType();
boolean isEnabled();
boolean updateActionBarTitle();
}
| CellularPrivacy/Android-IMSI-Catcher-Detector | AIMSICD/src/main/java/com/secupwn/aimsicd/ui/drawer/NavDrawerItem.java | Java | gpl-3.0 | 575 |
package view.rendes;
import java.awt.Graphics;
import java.awt.Shape;
import java.awt.geom.RoundRectangle2D;
import javax.swing.JTextField;
public class RoundJTextField extends JTextField {
private Shape shape;
public RoundJTextField(int size) {
super(size);
setOpaque(false); // As suggested by @AVD in comment.
}
protected void paintComponent(Graphics g) {
g.setColor(getBackground());
g.fillRoundRect(0, 0, getWidth()-1, getHeight()-1, 15, 15);
super.paintComponent(g);
}
protected void paintBorder(Graphics g) {
g.setColor(getForeground());
g.drawRoundRect(0, 0, getWidth()-1, getHeight()-1, 15, 15);
}
public boolean contains(int x, int y) {
if (shape == null || !shape.getBounds().equals(getBounds())) {
shape = new RoundRectangle2D.Float(0, 0, getWidth()-1, getHeight()-1, 15, 15);
}
return shape.contains(x, y);
}
} | gihon19/postHotelSonrisa | postHotelSonrisa/src/view/rendes/RoundJTextField.java | Java | gpl-3.0 | 992 |
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
package com.sun.star.sdbcx.comp.hsqldb;
import org.hsqldb.lib.FileAccess;
import org.hsqldb.lib.FileSystemRuntimeException;
@SuppressWarnings("ucd")
public class StorageFileAccess implements org.hsqldb.lib.FileAccess{
static { NativeLibraries.load(); }
String ds_name;
String key;
/** Creates a new instance of StorageFileAccess */
public StorageFileAccess(Object key) throws java.lang.Exception{
this.key = (String)key;
}
public void createParentDirs(String filename) {
}
public boolean isStreamElement(String elementName) {
return isStreamElement(key,elementName);
}
public java.io.InputStream openInputStreamElement(String streamName) throws java.io.IOException {
return new NativeInputStreamHelper(key,streamName);
}
public java.io.OutputStream openOutputStreamElement(String streamName) throws java.io.IOException {
return new NativeOutputStreamHelper(key,streamName);
}
public void removeElement(String filename) throws java.util.NoSuchElementException {
try {
if ( isStreamElement(key,filename) )
removeElement(key,filename);
} catch (java.io.IOException e) {
throw new FileSystemRuntimeException( e );
}
}
public void renameElement(String oldName, String newName) throws java.util.NoSuchElementException {
try {
if ( isStreamElement(key,oldName) ){
removeElement(key,newName);
renameElement(key,oldName, newName);
}
} catch (java.io.IOException e) {
throw new FileSystemRuntimeException( e );
}
}
private class FileSync implements FileAccess.FileSync
{
private final NativeOutputStreamHelper os;
private FileSync(NativeOutputStreamHelper _os)
{
os = _os;
}
public void sync() throws java.io.IOException
{
os.sync();
}
}
public FileAccess.FileSync getFileSync(java.io.OutputStream os) throws java.io.IOException
{
return new FileSync((NativeOutputStreamHelper)os);
}
static native boolean isStreamElement(String key,String elementName);
static native void removeElement(String key,String filename) throws java.util.NoSuchElementException, java.io.IOException;
static native void renameElement(String key,String oldName, String newName) throws java.util.NoSuchElementException, java.io.IOException;
}
| jvanz/core | connectivity/com/sun/star/sdbcx/comp/hsqldb/StorageFileAccess.java | Java | gpl-3.0 | 3,316 |
package cn.nukkit.redstone;
import cn.nukkit.block.Block;
import cn.nukkit.block.BlockRedstoneWire;
import cn.nukkit.block.BlockSolid;
import cn.nukkit.math.Vector3;
import java.util.*;
/**
* author: Angelic47
* Nukkit Project
*/
public class Redstone {
public static final int POWER_NONE = 0;
public static final int POWER_WEAKEST = 1;
public static final int POWER_STRONGEST = 16;
//NOTICE: Here POWER_STRONGEST is 16, not 15.
//I set it to 16 in order to calculate the energy in blocks, such as the redstone torch under the cobblestone.
//At that time, the cobblestone's energy is 16, not 15. If you put a redstone wire next to it, the redstone wire will got 15 energy.
//So, POWER_WEAKEST also means that energy in blocks, not redstone wire it self. So set it to 1.
private static final Comparator<UpdateObject> orderIsdn = new Comparator<UpdateObject>() {
@Override
public int compare(UpdateObject o1, UpdateObject o2) {
if (o1.getPopulation() > o2.getPopulation()) {
return -1;
} else if (o1.getPopulation() < o2.getPopulation()) {
return 1;
} else {
return 0;
}
}
};
public static void active(Block source) {
Queue<UpdateObject> updateQueue = new PriorityQueue<>(1, orderIsdn);
int currentLevel = source.getPowerLevel() - 1;
if (currentLevel <= 0) {
return;
}
addToQueue(updateQueue, source);
while (!updateQueue.isEmpty()) {
UpdateObject updatingObj = updateQueue.poll();
Block updating = updatingObj.getLocation();
currentLevel = updatingObj.getPopulation();
if (currentLevel > updating.getPowerLevel()) {
updating.setPowerLevel(currentLevel);
updating.getLevel().setBlock(updating, updating, true, true);
addToQueue(updateQueue, updating);
}
}
}
public static void active(Block source, Map<String, Block> allBlocks) {
Queue<UpdateObject> updateQueue = new PriorityQueue<>(1, orderIsdn);
int currentLevel = source.getPowerLevel() - 1;
if (currentLevel <= 0) {
return;
}
addToQueue(updateQueue, source);
while (!updateQueue.isEmpty()) {
UpdateObject updatingObj = updateQueue.poll();
Block updating = updatingObj.getLocation();
currentLevel = updatingObj.getPopulation();
if (currentLevel > updating.getPowerLevel()) {
updating.setPowerLevel(currentLevel);
updating.getLevel().setBlock(updating, updating, true, true);
if (allBlocks.containsKey(updating.getLocationHash())) {
allBlocks.remove(updating.getLocationHash());
}
addToQueue(updateQueue, updating);
}
}
}
public static void deactive(Block source, int updateLevel) {
//Step 1: find blocks which need to update
Queue<UpdateObject> updateQueue = new PriorityQueue<>(1, orderIsdn);
Queue<UpdateObject> sourceList = new PriorityQueue<>(1, orderIsdn);
Map<String, Block> updateMap = new HashMap<>();
Map<String, Block> closedMap = new HashMap<>();
int currentLevel = updateLevel;
if (currentLevel <= 0) {
return;
}
addToDeactiveQueue(updateQueue, source, closedMap, sourceList, currentLevel);
while (!updateQueue.isEmpty()) {
UpdateObject updateObject = updateQueue.poll();
Block updating = updateObject.getLocation();
currentLevel = updateObject.getPopulation();
if (currentLevel >= updating.getPowerLevel()) {
updating.setPowerLevel(0);
updateMap.put(updating.getLocationHash(), updating);
addToDeactiveQueue(updateQueue, updating, closedMap, sourceList, currentLevel);
} else {
sourceList.add(new UpdateObject(updating.getPowerLevel(), updating));
}
}
//Step 2: recalculate redstone power
while (!sourceList.isEmpty()) {
active(sourceList.poll().getLocation(), updateMap);
}
for (Block block : updateMap.values()) {
block.setPowerLevel(0);
block.getLevel().setBlock(block, block, true, true);
}
}
private static void addToQueue(Queue<UpdateObject> updateQueue, Block location) {
if (location.getPowerLevel() <= 0) {
return;
}
for (int side : new int[]{Vector3.SIDE_NORTH, Vector3.SIDE_SOUTH, Vector3.SIDE_EAST, Vector3.SIDE_WEST, Vector3.SIDE_UP, Vector3.SIDE_DOWN}) {
if (location.getSide(side) instanceof BlockRedstoneWire) {
updateQueue.add(new UpdateObject(location.getPowerLevel() - 1, location.getSide(side)));
}
}
if (location instanceof BlockRedstoneWire) {
Block block = location.getSide(Vector3.SIDE_UP);
if (!(block instanceof BlockSolid)) {
for (int side : new int[]{Vector3.SIDE_NORTH, Vector3.SIDE_SOUTH, Vector3.SIDE_EAST, Vector3.SIDE_WEST}) {
if (block.getSide(side) instanceof BlockRedstoneWire) {
updateQueue.add(new UpdateObject(location.getPowerLevel() - 1, block.getSide(side)));
}
}
}
for (int side : new int[]{Vector3.SIDE_NORTH, Vector3.SIDE_WEST, Vector3.SIDE_EAST, Vector3.SIDE_SOUTH}) {
block = location.getSide(side);
if (!(block instanceof BlockSolid)) {
Block blockDown;
blockDown = block.getSide(Vector3.SIDE_DOWN);
if (blockDown instanceof BlockRedstoneWire) {
updateQueue.add(new UpdateObject(location.getPowerLevel() - 1, blockDown));
}
}
}
}
}
private static void addToDeactiveQueue(Queue<UpdateObject> updateQueue, Block location, Map<String, Block> closedMap, Queue<UpdateObject> sourceList, int updateLevel) {
if (updateLevel < 0) {
return;
}
for (int side : new int[]{Vector3.SIDE_NORTH, Vector3.SIDE_SOUTH, Vector3.SIDE_EAST, Vector3.SIDE_WEST, Vector3.SIDE_UP, Vector3.SIDE_DOWN}) {
if (location.getSide(side).isPowerSource() || (updateLevel == 0 && location.getSide(side).isPowered())) {
sourceList.add(new UpdateObject(location.getPowerLevel(side), location.getSide(side)));
} else if (location.getSide(side) instanceof BlockRedstoneWire) {
if (!closedMap.containsKey(location.getSide(side).getLocationHash())) {
closedMap.put(location.getSide(side).getLocationHash(), location.getSide(side));
updateQueue.add(new UpdateObject(updateLevel - 1, location.getSide(side)));
}
}
}
if (location instanceof BlockRedstoneWire) {
Block block = location.getSide(Vector3.SIDE_UP);
for (int side : new int[]{Vector3.SIDE_NORTH, Vector3.SIDE_SOUTH, Vector3.SIDE_EAST, Vector3.SIDE_WEST}) {
if (block.getSide(side) instanceof BlockRedstoneWire) {
if (!closedMap.containsKey(block.getSide(side).getLocationHash())) {
closedMap.put(block.getSide(side).getLocationHash(), block.getSide(side));
updateQueue.add(new UpdateObject(updateLevel - 1, block.getSide(side)));
}
}
}
Block blockDown;
for (int side : new int[]{Vector3.SIDE_NORTH, Vector3.SIDE_SOUTH, Vector3.SIDE_EAST, Vector3.SIDE_WEST}) {
block = location.getSide(side);
blockDown = block.getSide(Vector3.SIDE_DOWN);
if (blockDown instanceof BlockRedstoneWire) {
if (!closedMap.containsKey(blockDown.getLocationHash())) {
closedMap.put(blockDown.getLocationHash(), blockDown);
updateQueue.add(new UpdateObject(updateLevel - 1, blockDown));
}
}
}
}
}
} | Niall7459/Nukkit | src/main/java/cn/nukkit/redstone/Redstone.java | Java | gpl-3.0 | 8,365 |
package net.minecraft.entity.monster;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityCreature;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
import net.minecraft.init.SoundEvents;
import net.minecraft.item.ItemAxe;
import net.minecraft.item.ItemStack;
import net.minecraft.util.DamageSource;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.SoundEvent;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.EnumDifficulty;
import net.minecraft.world.EnumSkyBlock;
import net.minecraft.world.World;
public abstract class EntityMob extends EntityCreature implements IMob
{
public EntityMob(World worldIn)
{
super(worldIn);
this.experienceValue = 5;
}
public SoundCategory getSoundCategory()
{
return SoundCategory.HOSTILE;
}
/**
* Called frequently so the entity can update its state every tick as required. For example, zombies and skeletons
* use this to react to sunlight and start to burn.
*/
public void onLivingUpdate()
{
this.updateArmSwingProgress();
float f = this.getBrightness(1.0F);
if (f > 0.5F)
{
this.entityAge += 2;
}
super.onLivingUpdate();
}
/**
* Called to update the entity's position/logic.
*/
public void onUpdate()
{
super.onUpdate();
if (!this.worldObj.isRemote && this.worldObj.getDifficulty() == EnumDifficulty.PEACEFUL)
{
this.setDead();
}
}
protected SoundEvent getSwimSound()
{
return SoundEvents.entity_hostile_swim;
}
protected SoundEvent getSplashSound()
{
return SoundEvents.entity_hostile_splash;
}
/**
* Called when the entity is attacked.
*/
public boolean attackEntityFrom(DamageSource source, float amount)
{
return this.isEntityInvulnerable(source) ? false : super.attackEntityFrom(source, amount);
}
protected SoundEvent getHurtSound()
{
return SoundEvents.entity_hostile_hurt;
}
protected SoundEvent getDeathSound()
{
return SoundEvents.entity_hostile_death;
}
protected SoundEvent getFallSound(int heightIn)
{
return heightIn > 4 ? SoundEvents.entity_hostile_big_fall : SoundEvents.entity_hostile_small_fall;
}
public boolean attackEntityAsMob(Entity entityIn)
{
float f = (float)this.getEntityAttribute(SharedMonsterAttributes.ATTACK_DAMAGE).getAttributeValue();
int i = 0;
if (entityIn instanceof EntityLivingBase)
{
f += EnchantmentHelper.getModifierForCreature(this.getHeldItemMainhand(), ((EntityLivingBase)entityIn).getCreatureAttribute());
i += EnchantmentHelper.getKnockbackModifier(this);
}
boolean flag = entityIn.attackEntityFrom(DamageSource.causeMobDamage(this), f);
if (flag)
{
if (i > 0 && entityIn instanceof EntityLivingBase)
{
((EntityLivingBase)entityIn).knockBack(this, (float)i * 0.5F, (double)MathHelper.sin(this.rotationYaw * 0.017453292F), (double)(-MathHelper.cos(this.rotationYaw * 0.017453292F)));
this.motionX *= 0.6D;
this.motionZ *= 0.6D;
}
int j = EnchantmentHelper.getFireAspectModifier(this);
if (j > 0)
{
entityIn.setFire(j * 4);
}
if (entityIn instanceof EntityPlayer)
{
EntityPlayer entityplayer = (EntityPlayer)entityIn;
ItemStack itemstack = this.getHeldItemMainhand();
ItemStack itemstack1 = entityplayer.isHandActive() ? entityplayer.getActiveItemStack() : null;
if (itemstack != null && itemstack1 != null && itemstack.getItem() instanceof ItemAxe && itemstack1.getItem() == Items.shield)
{
float f1 = 0.25F + (float)EnchantmentHelper.getEfficiencyModifier(this) * 0.05F;
if (this.rand.nextFloat() < f1)
{
entityplayer.getCooldownTracker().setCooldown(Items.shield, 100);
this.worldObj.setEntityState(entityplayer, (byte)30);
}
}
}
this.applyEnchantments(this, entityIn);
}
return flag;
}
public float getBlockPathWeight(BlockPos pos)
{
return 0.5F - this.worldObj.getLightBrightness(pos);
}
/**
* Checks to make sure the light is not too bright where the mob is spawning
*/
protected boolean isValidLightLevel()
{
BlockPos blockpos = new BlockPos(this.posX, this.getEntityBoundingBox().minY, this.posZ);
if (this.worldObj.getLightFor(EnumSkyBlock.SKY, blockpos) > this.rand.nextInt(32))
{
return false;
}
else
{
int i = this.worldObj.getLightFromNeighbors(blockpos);
if (this.worldObj.isThundering())
{
int j = this.worldObj.getSkylightSubtracted();
this.worldObj.setSkylightSubtracted(10);
i = this.worldObj.getLightFromNeighbors(blockpos);
this.worldObj.setSkylightSubtracted(j);
}
return i <= this.rand.nextInt(8);
}
}
/**
* Checks if the entity's current position is a valid location to spawn this entity.
*/
public boolean getCanSpawnHere()
{
return this.worldObj.getDifficulty() != EnumDifficulty.PEACEFUL && this.isValidLightLevel() && super.getCanSpawnHere();
}
protected void applyEntityAttributes()
{
super.applyEntityAttributes();
this.getAttributeMap().registerAttribute(SharedMonsterAttributes.ATTACK_DAMAGE);
}
/**
* Entity won't drop items or experience points if this returns false
*/
protected boolean canDropLoot()
{
return true;
}
} | aebert1/BigTransport | build/tmp/recompileMc/sources/net/minecraft/entity/monster/EntityMob.java | Java | gpl-3.0 | 6,264 |
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @author Vitaly A. Provodin
*/
/**
* Created on 18.02.2005
*/
package org.apache.harmony.jpda.tests.jdwp.ThreadReference;
import org.apache.harmony.jpda.tests.framework.DebuggeeSynchronizer;
import org.apache.harmony.jpda.tests.framework.LogWriter;
import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
import org.apache.harmony.jpda.tests.share.SyncDebuggee;
/**
* The class specifies debuggee for <code>org.apache.harmony.jpda.tests.jdwp.ThreadReference.ThreadGroupTest</code>.
* This debuggee is started as follow:
* <ol>
* <li>the tested group <code>TESTED_GROUP</code> is created
* <li>the tested thread <code>TESTED_THREAD</code> is started so this
* thread belongs to that thread group
* </ol>
* For different goals of tests, the debuggee sends the <code>SGNL_READY</code>
* signal to and waits for the <code>SGNL_CONTINUE</code> signal from debugger
* in two places:
* <ul>
* <li>right away when the tested thread has been started
* <li>when the tested thread has been finished
* </ul>
*/
public class ThreadGroupDebuggee extends SyncDebuggee {
public static final String TESTED_GROUP = "TestedGroup";
public static final String TESTED_THREAD = "TestedThread";
static Object waitForStart = new Object();
static Object waitForFinish = new Object();
static Object waitTimeObject = new Object();
static void waitMlsecsTime(long mlsecsTime) {
synchronized(waitTimeObject) {
try {
waitTimeObject.wait(mlsecsTime);
} catch (Throwable throwable) {
// ignore
}
}
}
public void run() {
ThreadGroup thrdGroup = new ThreadGroup(TESTED_GROUP);
DebuggeeThread thrd = new DebuggeeThread(thrdGroup, TESTED_THREAD,
logWriter, synchronizer);
synchronized(waitForStart){
thrd.start();
try {
waitForStart.wait();
} catch (InterruptedException e) {
}
}
while ( thrd.isAlive() ) {
waitMlsecsTime(100);
}
// synchronized(waitForFinish){
logWriter.println("thread is finished");
// }
logWriter.println("send SGNL_READY");
synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_READY);
synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
}
class DebuggeeThread extends Thread {
LogWriter logWriter;
DebuggeeSynchronizer synchronizer;
public DebuggeeThread(ThreadGroup thrdGroup, String name,
LogWriter logWriter, DebuggeeSynchronizer synchronizer) {
super(thrdGroup, name);
this.logWriter = logWriter;
this.synchronizer = synchronizer;
}
public void run() {
synchronized(ThreadGroupDebuggee.waitForFinish){
synchronized(ThreadGroupDebuggee.waitForStart){
ThreadGroupDebuggee.waitForStart.notifyAll();
logWriter.println(getName() + ": started");
synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_READY);
logWriter.println(getName() + ": wait for SGNL_CONTINUE");
synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
logWriter.println(getName() + ": finished");
}
}
}
}
public static void main(String [] args) {
runDebuggee(ThreadGroupDebuggee.class);
}
}
| s20121035/rk3288_android5.1_repo | external/apache-harmony/jdwp/src/test/java/org/apache/harmony/jpda/tests/jdwp/ThreadReference/ThreadGroupDebuggee.java | Java | gpl-3.0 | 4,431 |
/**
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations under
* the License.
*
* The Original Code is OpenELIS code.
*
* Copyright (C) The Minnesota Department of Health. All Rights Reserved.
*/
package us.mn.state.health.lims.codeelementtype.action;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import us.mn.state.health.lims.codeelementtype.dao.CodeElementTypeDAO;
import us.mn.state.health.lims.codeelementtype.daoimpl.CodeElementTypeDAOImpl;
import us.mn.state.health.lims.codeelementtype.valueholder.CodeElementType;
import us.mn.state.health.lims.common.action.BaseAction;
import us.mn.state.health.lims.common.action.BaseActionForm;
import us.mn.state.health.lims.common.exception.LIMSRuntimeException;
import us.mn.state.health.lims.common.log.LogEvent;
import us.mn.state.health.lims.common.util.StringUtil;
/**
* @author diane benz
*
* To change this generated comment edit the template variable "typecomment":
* Window>Preferences>Java>Templates. To enable and disable the creation of type
* comments go to Window>Preferences>Java>Code Generation.
*/
public class CodeElementTypeNextPreviousAction extends BaseAction {
private boolean isNew = false;
protected ActionForward performAction(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
// The first job is to determine if we are coming to this action with an
// ID parameter in the request. If there is no parameter, we are
// creating a new Analyte.
// If there is a parameter present, we should bring up an existing
// Analyte to edit.
String forward = FWD_SUCCESS;
request.setAttribute(ALLOW_EDITS_KEY, "true");
request.setAttribute(PREVIOUS_DISABLED, "false");
request.setAttribute(NEXT_DISABLED, "false");
String id = request.getParameter(ID);
if (StringUtil.isNullorNill(id) || "0".equals(id)) {
isNew = true;
} else {
isNew = false;
}
BaseActionForm dynaForm = (BaseActionForm) form;
String start = (String) request.getParameter("startingRecNo");
String direction = (String) request.getParameter("direction");
// System.out.println("This is ID from request " + id);
CodeElementType codeElementType = new CodeElementType();
codeElementType.setId(id);
try {
CodeElementTypeDAO codeElementTypeDAO = new CodeElementTypeDAOImpl();
//retrieve analyte by id since the name may have changed
codeElementTypeDAO.getData(codeElementType);
if (FWD_NEXT.equals(direction)) {
//bugzilla 1427 pass in name not id
List codeElementTypes = codeElementTypeDAO.getNextCodeElementTypeRecord(codeElementType.getText());
if (codeElementTypes != null && codeElementTypes.size() > 0) {
codeElementType = (CodeElementType) codeElementTypes.get(0);
codeElementTypeDAO.getData(codeElementType);
if (codeElementTypes.size() < 2) {
// disable next button
request.setAttribute(NEXT_DISABLED, "true");
}
id = codeElementType.getId();
} else {
// just disable next button
request.setAttribute(NEXT_DISABLED, "true");
}
}
if (FWD_PREVIOUS.equals(direction)) {
//bugzilla 1427 pass in name not id
List codeElementTypes = codeElementTypeDAO.getPreviousCodeElementTypeRecord(codeElementType.getText());
if (codeElementTypes != null && codeElementTypes.size() > 0) {
codeElementType = (CodeElementType) codeElementTypes.get(0);
codeElementTypeDAO.getData(codeElementType);
if (codeElementTypes.size() < 2) {
// disable previous button
request.setAttribute(PREVIOUS_DISABLED, "true");
}
id = codeElementType.getId();
} else {
// just disable next button
request.setAttribute(PREVIOUS_DISABLED, "true");
}
}
} catch (LIMSRuntimeException lre) {
//bugzilla 2154
LogEvent.logError("CodeElementTypeNextPreviousAction","performAction()",lre.toString());
request.setAttribute(ALLOW_EDITS_KEY, "false");
// disable previous and next
request.setAttribute(PREVIOUS_DISABLED, "true");
request.setAttribute(NEXT_DISABLED, "true");
forward = FWD_FAIL;
}
if (forward.equals(FWD_FAIL))
return mapping.findForward(forward);
if (codeElementType.getId() != null && !codeElementType.getId().equals("0")) {
request.setAttribute(ID, codeElementType.getId());
}
return getForward(mapping.findForward(forward), id, start);
}
protected String getPageTitleKey() {
return null;
}
protected String getPageSubtitleKey() {
return null;
}
} | mark47/OESandbox | app/src/us/mn/state/health/lims/codeelementtype/action/CodeElementTypeNextPreviousAction.java | Java | mpl-2.0 | 5,173 |
import java.util.Scanner;
public class SegmentTree {
private static class Node {
public int left, right;
public long add, sum;
public Node(int left, int right, long sum) {
this.left = left;
this.right = right;
this.sum = sum;
}
}
private Node[] tree;
private int size;
public SegmentTree(int n,int[] arr) {
size = (n<<2);
tree = new Node[size];
build(0, 0, n-1, arr);
}
private void build(int pos, int p, int r, int[] arr) {
if (p == r) {
tree[pos] = new Node(p, r, arr[p]);
} else {
build(2*pos+1, p, (p+r)/2, arr);
build(2*pos+2, (p+r)/2+1, r, arr);
tree[pos] = new Node(p, r, tree[2*pos+1].sum + tree[2*pos+2].sum);
}
}
public void update(int p, int r, long delt) {
p = (tree[0].left < p)?
p : tree[0].left;
r = (tree[0].right > r)?
r : tree[0].right;
if (p <= r) {
updateHelp(0, p, r, delt);
}
}
private void updateHelp(int pos, int p, int r, long delt) {
if (tree[pos].left>=p && tree[pos].right<=r) {
tree[pos].add += delt;
tree[pos].sum +=
(tree[pos].right-tree[pos].left+1)*delt;
} else {
if (tree[pos].add!=0) {
pushDown(pos);
}
int mid = (tree[pos].left+tree[pos].right)/2;
if (p <= mid) {
updateHelp(2*pos+1, p, r, delt);
}
if (mid+1 <= r) {
updateHelp(2*pos+2, p, r, delt);
}
tree[pos].sum = tree[2*pos+1].sum + tree[2*pos+2].sum;
}
}
private void pushDown(int pos) {
int left = 2*pos+1, right = 2*pos+2;
tree[left].add += tree[pos].add;
tree[right].add += tree[pos].add;
tree[left].sum +=
(tree[left].right-tree[left].left+1)*tree[pos].add;
tree[right].sum +=
(tree[right].right-tree[right].left+1)*tree[pos].add;
tree[pos].add = 0;
}
public long query(int p,int r) {
if (tree[0].left<=p && tree[0].right>=r) {
return queryHelp(0,p,r);
} else {
return 0;
}
}
private long queryHelp(int pos,int p,int r) {
if (tree[pos].left>=p && tree[pos].right<=r) {
return tree[pos].sum;
} else {
if (tree[pos].add!=0) {
pushDown(pos);
}
long val = 0;
int mid = (tree[pos].left+tree[pos].right)/2;
if (p <= mid) {
val += queryHelp(2*pos+1, p, r);
}
if (mid+1 <= r) {
val += queryHelp(2*pos+2, p, r);
}
return val;
}
}
public static void main(String[] args) {
Main.main(args);
}
}
class Main {
/** POJ 3468: http://poj.org/problem?id=3468 */
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] arr = new int[n];
int q = in.nextInt();
for (int i=0;i<n;i++) {
arr[i] = in.nextInt();
}
SegmentTree tr = new SegmentTree(n,arr);
for (int i=0;i<q;i++) {
String op = in.next();
if (op.equals("C")) {
int p = in.nextInt()-1;
int r = in.nextInt()-1;
tr.update(p,r,in.nextInt());
} else if (op.equals("Q")) {
int p = in.nextInt()-1;
int r = in.nextInt()-1;
System.out.println(tr.query(p,r));
}
}
in.close();
}
}
| DevinZ1993/Pieces-of-Code | java/Sets/src/SegmentTree.java | Java | mpl-2.0 | 3,871 |
/*
* This file is part of ToroDB.
*
* ToroDB is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ToroDB is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with ToroDB. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright (c) 2014, 8Kdata Technology
*
*/
package com.torodb.torod.core.language.querycriteria;
import com.torodb.torod.core.language.AttributeReference;
import com.torodb.torod.core.language.querycriteria.utils.QueryCriteriaVisitor;
import com.torodb.torod.core.subdocument.values.Value;
/**
*
*/
public class IsGreaterOrEqualQueryCriteria extends AttributeAndValueQueryCriteria {
private static final long serialVersionUID = 1L;
public IsGreaterOrEqualQueryCriteria(AttributeReference attributeReference, Value<?> val) {
super(attributeReference, val);
}
@Override
protected int getBaseHash() {
return 5;
}
@Override
public String toString() {
return getAttributeReference() + " >= " + getValue();
}
@Override
public <Result, Arg> Result accept(QueryCriteriaVisitor<Result, Arg> visitor, Arg arg) {
return visitor.visit(this, arg);
}
}
| ahachete/torodb | torod/torod-core/src/main/java/com/torodb/torod/core/language/querycriteria/IsGreaterOrEqualQueryCriteria.java | Java | agpl-3.0 | 1,681 |
/*
* This file is part of CoAnSys project.
* Copyright (c) 2012-2015 ICM-UW
*
* CoAnSys is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* CoAnSys is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with CoAnSys. If not, see <http://www.gnu.org/licenses/>.
*/
package pl.edu.icm.coansys.disambiguation.author.pig.merger;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.hadoop.mapreduce.Counter;
import org.apache.pig.EvalFunc;
import org.apache.pig.data.DataByteArray;
import org.apache.pig.data.DataType;
import org.apache.pig.data.Tuple;
import org.apache.pig.data.TupleFactory;
import org.apache.pig.impl.logicalLayer.FrontendException;
import org.apache.pig.impl.logicalLayer.schema.Schema;
import org.apache.pig.tools.pigstats.PigStatusReporter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pl.edu.icm.coansys.commons.java.DiacriticsRemover;
import pl.edu.icm.coansys.commons.java.StackTraceExtractor;
import pl.edu.icm.coansys.models.DocumentProtos.Author;
import pl.edu.icm.coansys.models.DocumentProtos.BasicMetadata;
import pl.edu.icm.coansys.models.DocumentProtos.DocumentMetadata;
import pl.edu.icm.coansys.models.DocumentProtos.DocumentWrapper;
import pl.edu.icm.coansys.models.DocumentProtos.KeyValue;
/**
*
* @author pdendek
*/
public class MergeDocumentWithOrcid extends EvalFunc<Tuple> {
PigStatusReporter myPigStatusReporter;
private static final Logger logger = LoggerFactory
.getLogger(MergeDocumentWithOrcid.class);
@Override
public Schema outputSchema(Schema p_input) {
try {
return Schema.generateNestedSchema(DataType.TUPLE,
DataType.CHARARRAY, DataType.BYTEARRAY);
} catch (FrontendException e) {
logger.error("Error in creating output schema:", e);
throw new IllegalStateException(e);
}
}
@Override
public Tuple exec(Tuple input) throws IOException {
if (input == null || input.size() != 3) {
return null;
}
try {
myPigStatusReporter = PigStatusReporter.getInstance();
//load input tuple
////doi
String docId = (String) input.get(0);
////normal document
DataByteArray dbaD = (DataByteArray) input.get(1);
////orcid document
DataByteArray dbaO = (DataByteArray) input.get(2);
//load input documents
DocumentWrapper dwD = DocumentWrapper.parseFrom(dbaD.get());
List<Author> aDL = dwD.getDocumentMetadata().getBasicMetadata().getAuthorList();
DocumentWrapper dwO = DocumentWrapper.parseFrom(dbaO.get());
List<Author> aOL = dwO.getDocumentMetadata().getBasicMetadata().getAuthorList();
//calculate merged author list
List<Author> aRL = matchAuthors(docId,aDL,aOL);
//construct resulting document
BasicMetadata.Builder bmR = BasicMetadata.newBuilder(DocumentWrapper.newBuilder(dwD).getDocumentMetadata().getBasicMetadata());
bmR.clearAuthor();
bmR.addAllAuthor(aRL);
DocumentMetadata.Builder dmR = DocumentMetadata.newBuilder(DocumentWrapper.newBuilder(dwD).getDocumentMetadata());
dmR.setBasicMetadata(bmR);
DocumentWrapper.Builder dwR = DocumentWrapper.newBuilder(dwD);
dwR.setDocumentMetadata(dmR);
//construct resulting tuple
Tuple result = TupleFactory.getInstance().newTuple();
result.append(docId);
result.append(new DataByteArray(dwR.build().toByteArray()));
return result;
} catch (Exception e) {
logger.error("Error in processing input row:", e);
throw new IOException("Caught exception processing input row:\n"
+ StackTraceExtractor.getStackTrace(e));
}
}
protected List<Author> matchAuthors(String docId, List<Author> base,
List<Author> second) {
List<Author> result = new ArrayList<Author>(base.size());
List<Author> secondCopy = new ArrayList<Author>(second);
boolean changedBln = false;
int changedInt = 0;
logger.error("-------------------------------------------");
logger.error("number of base authors: "+base.size()+"\tnumber of orcid authors");
for (Author author : base) {
Author foundAuthor = null;
for (Author secondAuthor : secondCopy) {
if (
equalsIgnoreCaseIgnoreDiacritics(author.getName(), secondAuthor.getName())
||
//equalsIgnoreCaseIgnoreDiacritics(author.getForenames(), secondAuthor.getForenames()) &&
equalsIgnoreCaseIgnoreDiacritics(author.getSurname(), secondAuthor.getSurname())
){
foundAuthor = secondAuthor;
break;
}
}
if (foundAuthor != null) {
result.add(merge(author,foundAuthor));
changedBln = true;
changedInt++;
if(myPigStatusReporter != null){
Counter c = myPigStatusReporter.getCounter("ORCID Enhancement", "Author Enhanced");
if(c!=null){
c.increment(1);
}
}
} else {
result.add(Author.newBuilder(author).build());
}
}
if(changedBln){
logger.info("------------------------------------------");
logger.info("Changed docId:"+docId);
if(myPigStatusReporter != null){
Counter c = myPigStatusReporter.getCounter("ORCID Enhancement", "Document Enhanced");
if(c!=null){
c.increment(1);
}
}
}
logger.error("number of intersections: "+changedInt);
return result;
}
private Author merge(Author author, Author foundAuthor) {
Author.Builder builder = Author.newBuilder(author);
for(KeyValue kv : foundAuthor.getExtIdList()){
if("orcid-author-id".equals(kv.getKey())){
KeyValue.Builder kvb = KeyValue.newBuilder();
kvb.setKey(kv.getKey());
kvb.setValue(kv.getValue());
builder.addExtId(kvb.build());
logger.info("<k:"+kv.getKey()+"; v:"+kv.getValue()+">");
logger.info("<kc:"+kvb.getKey()+"; vc:"+kvb.getValue()+">");
}
}
Author ret = builder.build();
logger.info("<auth:"+ret.toString()+">");
return ret;
}
private boolean equalsIgnoreCaseIgnoreDiacritics(String firstName,
String secondName) {
if (firstName.isEmpty() || secondName.isEmpty()) {
return false;
}
return DiacriticsRemover.removeDiacritics(firstName).equalsIgnoreCase(
DiacriticsRemover.removeDiacritics(secondName));
}
}
| acz-icm/coansys | disambiguation-author/disambiguation-author-logic/src/main/java/pl/edu/icm/coansys/disambiguation/author/pig/merger/MergeDocumentWithOrcid.java | Java | agpl-3.0 | 6,921 |
/*******************************************************************************
* This file is part of OpenNMS(R).
*
* Copyright (C) 2007-2014 The OpenNMS Group, Inc.
* OpenNMS(R) is Copyright (C) 1999-2014 The OpenNMS Group, Inc.
*
* OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
*
* OpenNMS(R) is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* OpenNMS(R) is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with OpenNMS(R). If not, see:
* http://www.gnu.org/licenses/
*
* For more information contact:
* OpenNMS(R) Licensing <license@opennms.org>
* http://www.opennms.org/
* http://www.opennms.com/
*******************************************************************************/
package org.opennms.web.command;
/**
* Command object for listing a specific statistics report. This object deserializes query params
* for a specific report, identified by integer ID.
*
* @author <a href="mailto:dj@opennms.org">DJ Gregor</a>
* @version $Id: $
* @since 1.8.1
*/
public class StatisticsReportCommand {
private Integer m_id;
/**
* <p>getId</p>
*
* @return a {@link java.lang.Integer} object.
*/
public Integer getId() {
return m_id;
}
/**
* <p>setId</p>
*
* @param id a {@link java.lang.Integer} object.
*/
public void setId(Integer id) {
m_id = id;
}
}
| roskens/opennms-pre-github | opennms-webapp/src/main/java/org/opennms/web/command/StatisticsReportCommand.java | Java | agpl-3.0 | 1,870 |
/*******************************************************************************
* This file is part of OpenNMS(R).
*
* Copyright (C) 2007-2014 The OpenNMS Group, Inc.
* OpenNMS(R) is Copyright (C) 1999-2014 The OpenNMS Group, Inc.
*
* OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
*
* OpenNMS(R) is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* OpenNMS(R) is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with OpenNMS(R). If not, see:
* http://www.gnu.org/licenses/
*
* For more information contact:
* OpenNMS(R) Licensing <license@opennms.org>
* http://www.opennms.org/
* http://www.opennms.com/
*******************************************************************************/
package org.opennms.core.tasks;
import org.springframework.util.Assert;
/**
* <p>AsyncTask class.</p>
*
* @author ranger
* @version $Id: $
*/
public class AsyncTask<T> extends AbstractTask {
private final Async<T> m_async;
private final Callback<T> m_callback;
/**
* <p>Constructor for AsyncTask.</p>
*
* @param coordinator a {@link org.opennms.core.tasks.TaskCoordinator} object.
* @param parent a {@link org.opennms.core.tasks.ContainerTask} object.
* @param async a {@link org.opennms.core.tasks.Async} object.
* @param <T> a T object.
*/
public AsyncTask(TaskCoordinator coordinator, ContainerTask<?> parent, Async<T> async) {
this(coordinator, parent, async, null);
}
/**
* <p>Constructor for AsyncTask.</p>
*
* @param coordinator a {@link org.opennms.core.tasks.TaskCoordinator} object.
* @param parent a {@link org.opennms.core.tasks.ContainerTask} object.
* @param async a {@link org.opennms.core.tasks.Async} object.
* @param callback a {@link org.opennms.core.tasks.Callback} object.
*/
public AsyncTask(TaskCoordinator coordinator, ContainerTask<?> parent, Async<T> async, Callback<T> callback) {
super(coordinator, parent);
Assert.notNull(async, "async parameter must not be null");
m_async = async;
m_callback = callback;
}
/** {@inheritDoc} */
@Override
public String toString() {
return String.valueOf(m_async);
}
/** {@inheritDoc} */
@Override
protected void doSubmit() {
Callback<T> callback = callback();
try {
m_async.supplyAsyncThenAccept(callback);
} catch (Throwable t) {
callback.handleException(t);
}
}
/**
* <p>markTaskAsCompleted</p>
*/
private final void markTaskAsCompleted() {
getCoordinator().markTaskAsCompleted(this);
}
private Callback<T> callback() {
return new Callback<T>() {
@Override
public void accept(T t) {
try {
if (m_callback != null) {
m_callback.accept(t);
}
} finally {
markTaskAsCompleted();
}
}
@Override
public T apply(Throwable t) {
try {
if (m_callback != null) {
m_callback.handleException(t);
}
} finally {
markTaskAsCompleted();
}
return null;
}
};
}
}
| aihua/opennms | core/tasks/src/main/java/org/opennms/core/tasks/AsyncTask.java | Java | agpl-3.0 | 3,889 |
package tc.oc.api.ocn;
import java.util.Collection;
import javax.inject.Singleton;
import com.google.common.util.concurrent.ListenableFuture;
import tc.oc.api.docs.MapRating;
import tc.oc.api.docs.virtual.MapDoc;
import tc.oc.api.docs.virtual.UserDoc;
import tc.oc.api.exceptions.NotFound;
import tc.oc.api.http.HttpOption;
import tc.oc.api.maps.MapRatingsRequest;
import tc.oc.api.maps.MapRatingsResponse;
import tc.oc.api.maps.MapService;
import tc.oc.api.maps.UpdateMapsResponse;
import tc.oc.api.model.HttpModelService;
import tc.oc.commons.core.concurrent.FutureUtils;
import tc.oc.commons.core.stream.Collectors;
@Singleton
class OCNMapService extends HttpModelService<MapDoc, MapDoc> implements MapService {
public ListenableFuture<Object> rate(MapRating rating) {
return this.client().post(memberUri(rating.map_id, "rate"), rating, Object.class, HttpOption.INFINITE_RETRY);
}
public ListenableFuture<MapRatingsResponse> getRatings(MapRatingsRequest request) {
return this.client().post(memberUri(request.map_id, "get_ratings"), request, MapRatingsResponse.class, HttpOption.INFINITE_RETRY);
}
public UpdateMapsResponse updateMaps(Collection<? extends MapDoc> maps) {
final ListenableFuture<MapUpdateMultiResponse> future = updateMulti(maps, MapUpdateMultiResponse.class);
return new UpdateMapsResponse(
(ListenableFuture) future,
maps.stream()
.flatMap(MapDoc::authorAndContributorUuids)
.distinct()
.collect(Collectors.mappingTo(uuid -> FutureUtils.mapSync(
future,
response -> {
final UserDoc.Identity user = response.users_by_uuid.get(uuid);
if(user != null) return user;
throw new NotFound();
}
)))
);
}
}
| OvercastNetwork/ProjectAres | API/ocn/src/main/java/tc/oc/api/ocn/OCNMapService.java | Java | agpl-3.0 | 1,908 |
/*
* This library is part of OpenCms -
* the Open Source Content Management System
*
* Copyright (c) Alkacon Software GmbH (http://www.alkacon.com)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* For further information about Alkacon Software GmbH, please see the
* company website: http://www.alkacon.com
*
* For further information about OpenCms, please see the
* project website: http://www.opencms.org
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.opencms.workplace.tools.content;
import org.opencms.file.CmsProperty;
import org.opencms.file.CmsPropertyDefinition;
import org.opencms.file.CmsResource;
import org.opencms.file.CmsVfsException;
import org.opencms.i18n.CmsMessages;
import org.opencms.jsp.CmsJspActionElement;
import org.opencms.lock.CmsLock;
import org.opencms.main.CmsException;
import org.opencms.main.OpenCms;
import org.opencms.workplace.CmsDialog;
import org.opencms.workplace.CmsWorkplace;
import org.opencms.workplace.CmsWorkplaceSettings;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
/**
* Provides methods for the delete property definition dialog.<p>
*
* @since 6.0.0
*/
public class CmsPropertyDelete extends CmsDialog {
/** Value for the action: delete cascade. */
public static final int ACTION_DELETE_CASCADE = 100;
/** Request parameter value for the action: delete cascade. */
public static final String DIALOG_DELETE_CASCADE = "deletecascade";
/** The dialog type. */
public static final String DIALOG_TYPE = "propertydelete";
/** Request parameter name for the property name. */
public static final String PARAM_PROPERTYNAME = "propertyname";
private String m_paramPropertyName;
/**
* Public constructor with JSP action element.<p>
*
* @param jsp an initialized JSP action element
*/
public CmsPropertyDelete(CmsJspActionElement jsp) {
super(jsp);
}
/**
* Public constructor with JSP variables.<p>
*
* @param context the JSP page context
* @param req the JSP request
* @param res the JSP response
*/
public CmsPropertyDelete(PageContext context, HttpServletRequest req, HttpServletResponse res) {
this(new CmsJspActionElement(context, req, res));
}
/**
* Deletes the property definition.<p>
*
* @throws JspException if problems including sub-elements occur
*/
public void actionDelete() throws JspException {
// save initialized instance of this class in request attribute for included sub-elements
getJsp().getRequest().setAttribute(SESSION_WORKPLACE_CLASS, this);
try {
getCms().deletePropertyDefinition(getParamPropertyName());
// close the dialog
actionCloseDialog();
} catch (Throwable e) {
// error while deleting property definition, show error dialog
includeErrorpage(this, e);
}
}
/**
* Deletes the property definition by cascading the properties on resources.<p>
*
* @throws JspException if problems including sub-elements occur
*/
public void actionDeleteCascade() throws JspException {
// save initialized instance of this class in request attribute for included sub-elements
getJsp().getRequest().setAttribute(SESSION_WORKPLACE_CLASS, this);
try {
// list of all resources containing this propertydefinition
List resourcesWithProperty = getCms().readResourcesWithProperty(getParamPropertyName());
// list of all resources locked by another user, containing this propertydefinition
List resourcesLockedByOtherUser = getResourcesLockedByOtherUser(resourcesWithProperty);
// do the following operations only if all of the resources are not locked by another user
if (resourcesLockedByOtherUser.isEmpty()) {
// save the site root
String storedSiteRoot = getCms().getRequestContext().getSiteRoot();
try {
// change to the root site
getCms().getRequestContext().setSiteRoot("/");
Iterator i = resourcesWithProperty.iterator();
while (i.hasNext()) {
CmsResource resource = (CmsResource)i.next();
// read the property object
CmsProperty property = getCms().readPropertyObject(
resource.getRootPath(),
getParamPropertyName(),
false);
// try to delete the property if it is not the NULL PROPERTY
// if the property is the NULL PROPERTY, it only had a shared
// value which was deleted at a sibling which was already processed
if (!property.isNullProperty()) {
CmsLock lock = getCms().getLock(resource);
if (lock.isUnlocked()) {
// lock the resource for the current (Admin) user
getCms().lockResource(resource.getRootPath());
}
property.setStructureValue(CmsProperty.DELETE_VALUE);
property.setResourceValue(CmsProperty.DELETE_VALUE);
// write the property with the null value to the resource and cascade it from the definition
getCms().writePropertyObject(resource.getRootPath(), property);
// unlock the resource
getCms().unlockResource(resource.getRootPath());
}
}
// delete the property definition at last
getCms().deletePropertyDefinition(getParamPropertyName());
} finally {
// restore the siteroot
getCms().getRequestContext().setSiteRoot(storedSiteRoot);
// close the dialog
actionCloseDialog();
}
} else {
StringBuffer reason = new StringBuffer();
reason.append(dialogWhiteBoxStart());
reason.append(buildResourceList(resourcesLockedByOtherUser, true));
reason.append(dialogWhiteBoxEnd());
throw new CmsVfsException(Messages.get().container(
Messages.ERR_DEL_PROP_RESOURCES_LOCKED_1,
reason.toString()));
}
} catch (Throwable e) {
// error while deleting property definition, show error dialog
includeErrorpage(this, e);
}
}
/**
* Builds a HTML list of Resources that use the specified property.<p>
*
* @throws CmsException if operation was not successful
*
* @return the HTML String for the Resource list
*/
public String buildResourceList() throws CmsException {
List resourcesWithProperty = getCms().readResourcesWithProperty(getParamPropertyName());
return buildResourceList(resourcesWithProperty, false);
}
/**
* Builds a HTML list of Resources.<p>
*
* Columns: Type, Name, Uri, Value of the property, locked by(optional).<p>
*
* @param resourceList a list of resources
* @param lockInfo a boolean to decide if the locked info should be shown or not
* @throws CmsException if operation was not successful
*
* @return the HTML String for the Resource list
*/
public String buildResourceList(List resourceList, boolean lockInfo) throws CmsException {
// reverse the resource list
Collections.reverse(resourceList);
CmsMessages messages = Messages.get().getBundle(getLocale());
StringBuffer result = new StringBuffer();
result.append("<table border=\"0\" width=\"100%\" cellpadding=\"1\" cellspacing=\"1\">\n");
result.append("<tr>\n");
// Type
result.append("\t<td style=\"width:5%;\" class=\"textbold\">");
result.append(messages.key(Messages.GUI_INPUT_TYPE_0));
result.append("</td>\n");
// Uri
result.append("\t<td style=\"width:40%;\" class=\"textbold\">");
result.append(messages.key(Messages.GUI_INPUT_ADRESS_0));
result.append("</td>\n");
// Name
result.append("\t<td style=\"width:25%;\" class=\"textbold\">");
result.append(messages.key(Messages.GUI_INPUT_TITLE_0));
result.append("</td>\n");
if (!lockInfo) {
// Property value
result.append("\t<td style=\"width:30%;\" class=\"textbold\">");
result.append(messages.key(Messages.GUI_INPUT_PROPERTYVALUE_0));
result.append("</td>\n");
}
if (lockInfo) {
// Property value
result.append("\t<td style=\"width:30%;\" class=\"textbold\">");
result.append(messages.key(Messages.GUI_EXPLORER_LOCKEDBY_0));
result.append("</td>\n");
result.append("</tr>\n");
}
result.append("</tr>\n");
result.append("<tr><td colspan=\"4\"><span style=\"height: 6px;\"> </span></td></tr>\n");
String storedSiteRoot = getCms().getRequestContext().getSiteRoot();
try {
getCms().getRequestContext().setSiteRoot("/");
Iterator i = resourceList.iterator();
while (i.hasNext()) {
CmsResource resource = (CmsResource)i.next();
String filetype = OpenCms.getResourceManager().getResourceType(resource.getTypeId()).getTypeName();
result.append("<tr>\n");
// file type
result.append("\t<td>");
result.append("<img src=\"");
result.append(getSkinUri());
result.append(CmsWorkplace.RES_PATH_FILETYPES);
result.append(filetype);
result.append(".gif\">");
result.append("</td>\n");
// file address
result.append("\t<td>");
result.append(resource.getRootPath());
result.append("</td>\n");
// title
result.append("\t<td>");
result.append(getJsp().property(CmsPropertyDefinition.PROPERTY_TITLE, resource.getRootPath(), ""));
result.append("</td>\n");
// current value of the property
if (!lockInfo) {
result.append("\t<td>");
result.append(getJsp().property(getParamPropertyName(), resource.getRootPath()));
result.append("</td>\n");
}
// locked by user
if (lockInfo) {
CmsLock lock = getCms().getLock(resource);
result.append("\t<td>");
result.append(getCms().readUser(lock.getUserId()).getName());
result.append("</td>\n");
}
result.append("</tr>\n");
}
result.append("</table>\n");
} finally {
getCms().getRequestContext().setSiteRoot(storedSiteRoot);
}
return result.toString();
}
/**
* Builds the html for the property definition select box.<p>
*
* @param attributes optional attributes for the <select> tag
* @return the html for the property definition select box
*/
public String buildSelectProperty(String attributes) {
return CmsPropertyChange.buildSelectProperty(getCms(), Messages.get().getBundle(getLocale()).key(
Messages.GUI_PLEASE_SELECT_0), attributes, "");
}
/**
* Returns the value of the propertyname parameter.<p>
*
* @return the value of the propertyname parameter
*/
public String getParamPropertyName() {
return m_paramPropertyName;
}
/**
* Sets the value of the propertyname parameter.<p>
*
* @param paramPropertyName the value of the propertyname parameter
*/
public void setParamPropertyName(String paramPropertyName) {
m_paramPropertyName = paramPropertyName;
}
/**
* @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
*/
protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest request) {
// fill the parameter values in the get/set methods
fillParamValues(request);
// set the dialog type
setParamDialogtype(DIALOG_TYPE);
// set the action for the JSP switch
if (DIALOG_OK.equals(getParamAction())) {
setAction(ACTION_OK);
setParamTitle(Messages.get().getBundle(getLocale()).key(Messages.GUI_TITLE_PROPERTYDELETE_0)
+ ": "
+ getParamPropertyName());
} else if (DIALOG_CANCEL.equals(getParamAction())) {
setAction(ACTION_CANCEL);
} else if (DIALOG_DELETE_CASCADE.equals(getParamAction())) {
setAction(ACTION_DELETE_CASCADE);
} else {
setAction(ACTION_DEFAULT);
// build title for change property value dialog
setParamTitle(Messages.get().getBundle(getLocale()).key(Messages.GUI_TITLE_PROPERTYDELETE_0));
}
}
/**
* Returns a list of resources that are locked by another user as the current user.<p>
*
* @param resourceList the list of all (mixed) resources
*
* @return a list of resources that are locked by another user as the current user
* @throws CmsException if the getLock operation fails
*/
private List getResourcesLockedByOtherUser(List resourceList) throws CmsException {
List lockedResourcesByOtherUser = new ArrayList();
Iterator i = resourceList.iterator();
while (i.hasNext()) {
CmsResource resource = (CmsResource)i.next();
// get the lock state for the resource
CmsLock lock = getCms().getLock(resource);
// add this resource to the list if this is locked by another user
if (!lock.isUnlocked() && !lock.isOwnedBy(getCms().getRequestContext().getCurrentUser())) {
lockedResourcesByOtherUser.add(resource);
}
}
return lockedResourcesByOtherUser;
}
}
| serrapos/opencms-core | src-modules/org/opencms/workplace/tools/content/CmsPropertyDelete.java | Java | lgpl-2.1 | 15,467 |
package org.auscope.portal.core.util;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.xml.XMLConstants;
import javax.xml.namespace.NamespaceContext;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.auscope.portal.core.test.PortalTestClass;
import org.junit.Assert;
import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;
/**
* Unit tests for DOMUtil
*
* @author Josh Vote
*
*/
public class TestDOMUtil extends PortalTestClass {
/**
* Simple test to ensure that the 2 DOM util methods are reversible
* @throws SAXException
* @throws IOException
* @throws ParserConfigurationException
* @throws TransformerException
*/
@Test
public void testReversibleTransformation() throws ParserConfigurationException, IOException, SAXException, TransformerException {
final String originalXmlString = ResourceUtil
.loadResourceAsString("org/auscope/portal/core/test/xml/TestXML_NoPrettyPrint.xml");
final Document doc = DOMUtil.buildDomFromString(originalXmlString);
final String newXmlString = DOMUtil.buildStringFromDom(doc, false);
Assert.assertEquals(originalXmlString, newXmlString);
}
/**
* Namespace for use with src/test/resources/TestXML_NoPrettyPrint.xml
*
* @author vot002
*
*/
public class SimpleXMLNamespace implements NamespaceContext {
private Map<String, String> map;
public SimpleXMLNamespace() {
map = new HashMap<>();
map.put("test", "http://test.namespace");
map.put("test2", "http://test2.namespace");
}
/**
* This method returns the uri for all prefixes needed.
*
* @param prefix
* @return uri
*/
@Override
public String getNamespaceURI(final String prefix) {
if (prefix == null)
throw new IllegalArgumentException("No prefix provided!");
if (map.containsKey(prefix))
return map.get(prefix);
else
return XMLConstants.NULL_NS_URI;
}
@Override
public String getPrefix(final String namespaceURI) {
// Not needed in this context.
return null;
}
@Override
public Iterator<String> getPrefixes(final String namespaceURI) {
// Not needed in this context.
return null;
}
}
/**
* Simple test to ensure that the DOM object is namespace aware
* @throws XPathExpressionException
* @throws IOException
* @throws SAXException
* @throws ParserConfigurationException
*/
@Test
public void testDOMObjectNamespace() throws XPathExpressionException, IOException, ParserConfigurationException, SAXException {
//Build our DOM
final String originalXmlString = ResourceUtil
.loadResourceAsString("org/auscope/portal/core/test/xml/TestXML_NoPrettyPrint.xml");
final Document doc = DOMUtil.buildDomFromString(originalXmlString);
//Build our queries (namespace aware)
final XPathFactory factory = XPathFactory.newDefaultInstance();
final XPath xPath = factory.newXPath();
xPath.setNamespaceContext(new SimpleXMLNamespace());
final XPathExpression getChild1Expr = xPath.compile("test:root/test2:child1");
final XPathExpression getChild2Expr = xPath.compile("test:root/test2:child2");
final XPathExpression failingExpr = xPath.compile("root/child1");
Node testNode = (Node) getChild1Expr.evaluate(doc, XPathConstants.NODE);
Assert.assertNotNull(testNode);
Assert.assertEquals("child1Value", testNode.getTextContent());
testNode = (Node) getChild2Expr.evaluate(doc, XPathConstants.NODE);
Assert.assertNotNull(testNode);
Assert.assertEquals("child2Value", testNode.getTextContent());
//This should fail (no namespace specified)
testNode = (Node) failingExpr.evaluate(doc, XPathConstants.NODE);
Assert.assertNull(testNode);
}
}
| jia020/portal-core | src/test/java/org/auscope/portal/core/util/TestDOMUtil.java | Java | lgpl-3.0 | 4,481 |
package org.auscope.portal.core.services.responses.vocab;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathException;
import org.auscope.portal.core.services.namespaces.VocabNamespaceContext;
import org.auscope.portal.core.test.PortalTestClass;
import org.auscope.portal.core.util.DOMUtil;
import org.auscope.portal.core.util.ResourceUtil;
import org.junit.Assert;
import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;
/**
* Unit tests for ConceptFactory
*
* @author Josh Vote
*
*/
public class TestConceptFactory extends PortalTestClass {
private void assertSameConcept(Concept[] expected, Concept[] actual, List<String> traversedUrns) {
String errMsg = String.format("%1$s != %2$s", Arrays.toString(expected), Arrays.toString(actual));
Assert.assertArrayEquals(errMsg, expected, actual);
for (int i = 0; i < expected.length; i++) {
assertSameConcept(expected[i], actual[i], traversedUrns);
}
}
private void assertSameConcept(Concept expected, Concept actual, List<String> traversedUrns) {
String errMsg = String.format("%1$s != %2$s", expected, actual);
Assert.assertEquals(errMsg, expected, actual);
Assert.assertEquals(errMsg, expected.getLabel(), actual.getLabel());
Assert.assertEquals(errMsg, expected.getPreferredLabel(), actual.getPreferredLabel());
Assert.assertEquals(errMsg, expected.isHref(), actual.isHref());
Assert.assertEquals(errMsg, expected.getDefinition(), actual.getDefinition());
//To deal with cycles in the hierarchy
if (traversedUrns.contains(expected.getUrn())) {
return;
} else {
traversedUrns.add(expected.getUrn());
}
assertSameConcept(expected.getBroader(), actual.getBroader(), traversedUrns);
assertSameConcept(expected.getNarrower(), actual.getNarrower(), traversedUrns);
assertSameConcept(expected.getRelated(), actual.getRelated(), traversedUrns);
}
/**
* Runs the factory through a standard SISSVoc response XML
* @throws IOException
* @throws SAXException
* @throws ParserConfigurationException
* @throws XPathException
*/
@Test
public void testSISSVocRDF() throws IOException, ParserConfigurationException, SAXException, XPathException {
//Build our expectation
Concept concept1 = new Concept("urn:concept:1");
Concept concept2 = new Concept("urn:concept:2");
Concept concept3 = new Concept("urn:concept:3");
Concept concept4 = new Concept("urn:concept:4");
NamedIndividual ni1 = new NamedIndividual("urn:ni:1");
NamedIndividual ni2 = new NamedIndividual("urn:ni:2");
NamedIndividual ni3 = new NamedIndividual("urn:ni:3");
concept1.setNarrower(new Concept[] {concept2, concept3, ni2});
concept1.setLabel("LabelConcept1");
concept1.setPreferredLabel("PrefLabelConcept1");
concept2.setBroader(new Concept[] {concept1});
concept2.setRelated(new Concept[] {concept3});
concept2.setLabel("LabelConcept2");
concept2.setPreferredLabel("PrefLabelConcept2");
concept2.setDefinition("DefinitionConcept2");
concept3.setBroader(new Concept[] {concept1});
concept3.setRelated(new Concept[] {concept2});
concept3.setNarrower(new Concept[] {ni1});
concept3.setLabel("LabelConcept3");
concept3.setPreferredLabel("PrefLabelConcept3");
concept4.setNarrower(new Concept[] {ni3});
concept4.setLabel("LabelConcept4");
concept4.setPreferredLabel("PrefLabelConcept4");
concept4.setDefinition("DefinitionConcept4");
ni1.setBroader(new Concept[] {concept3});
ni1.setLabel("LabelNamedIndividual1");
ni1.setPreferredLabel("PrefLabelNamedIndividual1");
ni2.setBroader(new Concept[] {concept1});
ni2.setLabel("LabelNamedIndividual2");
ni2.setPreferredLabel("PrefLabelNamedIndividual2");
ni3.setBroader(new Concept[] {concept4});
ni3.setLabel("LabelNamedIndividual3");
ni3.setPreferredLabel("PrefLabelNamedIndividual3");
Concept[] expectation = new Concept[] {concept1, concept4};
//Build our actual list
String responseXml = ResourceUtil
.loadResourceAsString("org/auscope/portal/core/test/responses/sissvoc/SISSVocResponse.xml");
Document responseDoc = DOMUtil.buildDomFromString(responseXml);
Node rdfNode = (Node) DOMUtil.compileXPathExpr("rdf:RDF", new VocabNamespaceContext()).evaluate(responseDoc,
XPathConstants.NODE);
ConceptFactory cf = new ConceptFactory();
Concept[] actualConcepts = cf.parseFromRDF(rdfNode);
Assert.assertNotNull(actualConcepts);
assertSameConcept(expectation, actualConcepts, new ArrayList<String>());
}
/**
* This is a legacy test for the older vocabularyServiceResponse.xml
*
* It tests our concepts still return EVEN if we don't define top level concepts
* @throws IOException
* @throws SAXException
* @throws ParserConfigurationException
* @throws XPathException
*/
@Test
public void testGetConcepts() throws IOException, ParserConfigurationException, SAXException, XPathException {
String responseXml = ResourceUtil
.loadResourceAsString("org/auscope/portal/core/test/responses/sissvoc/vocabularyServiceResponse.xml");
Document responseDoc = DOMUtil.buildDomFromString(responseXml);
Node rdfNode = (Node) DOMUtil.compileXPathExpr("rdf:RDF", new VocabNamespaceContext()).evaluate(responseDoc,
XPathConstants.NODE);
ConceptFactory cf = new ConceptFactory();
Concept[] actualConcepts = cf.parseFromRDF(rdfNode);
Assert.assertEquals("There are 27 concepts", 27, actualConcepts.length);
//Must contain: Siltstone - concrete aggregate
boolean found = false;
for (Concept concept : actualConcepts) {
if (concept.getPreferredLabel().equals("Siltstone - concrete aggregate")) {
found = true;
break;
}
}
Assert.assertTrue("Must contain: Siltstone - concrete aggregate", found);
//Must contain: Gneiss - crusher dust
found = false;
for (Concept concept : actualConcepts) {
if (concept.getPreferredLabel().equals("Gneiss - crusher dust")) {
found = true;
break;
}
}
Assert.assertTrue("Must contain: Gneiss - crusher dust", found);
}
}
| jia020/portal-core | src/test/java/org/auscope/portal/core/services/responses/vocab/TestConceptFactory.java | Java | lgpl-3.0 | 6,879 |
/**
* This file is part of FNLP (formerly FudanNLP).
*
* FNLP is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FNLP is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with FudanNLP. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2009-2014 www.fnlp.org. All rights reserved.
*/
package org.fnlp.util.exception;
import java.io.FileNotFoundException;
import java.io.IOException;
public class LoadModelException extends Exception {
private static final long serialVersionUID = -3933859344026018386L;
public LoadModelException(Exception e, String file) {
super(e);
if( e instanceof FileNotFoundException) {
System.out.println("模型文件不存在: "+ file);
} else if (e instanceof ClassNotFoundException) {
System.out.println("模型文件版本错误。");
} else if (e instanceof IOException) {
System.out.println("模型文件读入错误: "+file);
}
e.printStackTrace();
}
public LoadModelException(String msg) {
super(msg);
printStackTrace();
}
} | xpqiu/fnlp | fnlp-core/src/main/java/org/fnlp/util/exception/LoadModelException.java | Java | lgpl-3.0 | 1,512 |
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.activemq.artemis.common;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import org.apache.activemq.artemis.api.config.ActiveMQDefaultConfiguration;
import org.apache.activemq.artemis.api.core.TransportConfiguration;
import org.apache.activemq.artemis.api.core.client.ActiveMQClient;
import org.apache.activemq.artemis.api.core.client.ClientMessage;
import org.apache.activemq.artemis.api.core.client.ClientRequestor;
import org.apache.activemq.artemis.api.core.client.ClientSession;
import org.apache.activemq.artemis.api.core.client.ClientSessionFactory;
import org.apache.activemq.artemis.api.core.client.ServerLocator;
import org.apache.activemq.artemis.api.core.management.ManagementHelper;
import org.apache.activemq.artemis.api.core.management.ResourceNames;
import org.apache.activemq.artemis.core.remoting.impl.netty.NettyConnectorFactory;
import org.apache.activemq.artemis.tests.util.SpawnedVMSupport;
import org.junit.Assert;
import org.objectweb.jtests.jms.admin.Admin;
/**
* AbstractAdmin.
*/
public class AbstractAdmin implements Admin {
protected ClientSession clientSession;
protected ClientRequestor requestor;
protected boolean serverLifeCycleActive;
protected Process serverProcess;
protected ServerLocator serverLocator;
protected ClientSessionFactory sf;
// this is a constant to control if we should use a separate VM for the server.
public static final boolean spawnServer = false;
/**
* Determines whether to act or 'no-op' on serverStart() and
* serverStop(). This is used when testing combinations of client and
* servers with different versions.
*/
private static final String SERVER_LIVE_CYCLE_PROPERTY = "org.apache.activemq.artemis.jms.ActiveMQAMQPAdmin.serverLifeCycle";
public AbstractAdmin() {
serverLifeCycleActive = Boolean.valueOf(System.getProperty(SERVER_LIVE_CYCLE_PROPERTY, "true"));
}
@Override
public String getName() {
return getClass().getName();
}
@Override
public void start() throws Exception {
serverLocator = ActiveMQClient.createServerLocatorWithoutHA(new TransportConfiguration(NettyConnectorFactory.class.getName()));
sf = serverLocator.createSessionFactory();
clientSession = sf.createSession(ActiveMQDefaultConfiguration.getDefaultClusterUser(), ActiveMQDefaultConfiguration.getDefaultClusterPassword(), false, true, true, false, 1);
requestor = new ClientRequestor(clientSession, ActiveMQDefaultConfiguration.getDefaultManagementAddress());
clientSession.start();
}
@Override
public void stop() throws Exception {
requestor.close();
if (sf != null) {
sf.close();
}
if (serverLocator != null) {
serverLocator.close();
}
sf = null;
serverLocator = null;
}
@Override
public Context createContext() throws NamingException {
return new InitialContext();
}
@Override
public void createConnectionFactory(final String name) {
throw new RuntimeException("FIXME NYI createConnectionFactory");
}
@Override
public void deleteConnectionFactory(final String name) {
throw new RuntimeException("FIXME NYI deleteConnectionFactory");
}
@Override
public void createQueue(final String name) {
Boolean result;
try {
result = (Boolean) invokeSyncOperation(ResourceNames.JMS_SERVER, "createQueue", name, name);
Assert.assertEquals(true, result.booleanValue());
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
@Override
public void deleteQueue(final String name) {
Boolean result;
try {
result = (Boolean) invokeSyncOperation(ResourceNames.JMS_SERVER, "destroyQueue", name);
Assert.assertEquals(true, result.booleanValue());
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
@Override
public void createQueueConnectionFactory(final String name) {
createConnectionFactory(name);
}
@Override
public void deleteQueueConnectionFactory(final String name) {
deleteConnectionFactory(name);
}
@Override
public void createTopic(final String name) {
Boolean result;
try {
result = (Boolean) invokeSyncOperation(ResourceNames.JMS_SERVER, "createTopic", name, name);
Assert.assertEquals(true, result.booleanValue());
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
@Override
public void deleteTopic(final String name) {
Boolean result;
try {
result = (Boolean) invokeSyncOperation(ResourceNames.JMS_SERVER, "destroyTopic", name);
Assert.assertEquals(true, result.booleanValue());
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
@Override
public void createTopicConnectionFactory(final String name) {
createConnectionFactory(name);
}
@Override
public void deleteTopicConnectionFactory(final String name) {
deleteConnectionFactory(name);
}
@Override
public void startServer() throws Exception {
if (!serverLifeCycleActive) {
return;
}
if (spawnServer) {
String[] vmArgs = new String[]{};
serverProcess = SpawnedVMSupport.spawnVM(SpawnedJMSServer.class.getName(), vmArgs, false);
InputStreamReader isr = new InputStreamReader(serverProcess.getInputStream());
final BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null) {
System.out.println("SERVER: " + line);
if ("OK".equals(line.trim())) {
new Thread() {
@Override
public void run() {
try {
String line1 = null;
while ((line1 = br.readLine()) != null) {
System.out.println("SERVER: " + line1);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
return;
} else if ("KO".equals(line.trim())) {
// something went wrong with the server, destroy it:
serverProcess.destroy();
throw new IllegalStateException("Unable to start the spawned server :" + line);
}
}
} else {
SpawnedJMSServer.startServer();
}
}
@Override
public void stopServer() throws Exception {
if (!serverLifeCycleActive) {
return;
}
if (spawnServer) {
OutputStreamWriter osw = new OutputStreamWriter(serverProcess.getOutputStream());
osw.write("STOP\n");
osw.flush();
int exitValue = serverProcess.waitFor();
if (exitValue != 0) {
serverProcess.destroy();
}
} else {
SpawnedJMSServer.stopServer();
}
}
protected Object invokeSyncOperation(final String resourceName,
final String operationName,
final Object... parameters) throws Exception {
ClientMessage message = clientSession.createMessage(false);
ManagementHelper.putOperationInvocation(message, resourceName, operationName, parameters);
ClientMessage reply;
try {
reply = requestor.request(message, 3000);
} catch (Exception e) {
throw new IllegalStateException("Exception while invoking " + operationName + " on " + resourceName, e);
}
if (reply == null) {
throw new IllegalStateException("no reply received when invoking " + operationName + " on " + resourceName);
}
if (!ManagementHelper.hasOperationSucceeded(reply)) {
throw new IllegalStateException("operation failed when invoking " + operationName +
" on " +
resourceName +
": " +
ManagementHelper.getResult(reply));
}
return ManagementHelper.getResult(reply);
}
}
| okalmanRH/jboss-activemq-artemis | tests/joram-tests/src/test/java/org/apache/activemq/artemis/common/AbstractAdmin.java | Java | apache-2.0 | 9,253 |
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.qpid.jms.integration;
import static org.apache.qpid.jms.provider.amqp.AmqpSupport.ANONYMOUS_RELAY;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.util.UUID;
import javax.jms.JMSContext;
import javax.jms.JMSProducer;
import org.apache.qpid.jms.test.QpidJmsTestCase;
import org.apache.qpid.jms.test.testpeer.TestAmqpPeer;
import org.apache.qpid.proton.amqp.Binary;
import org.apache.qpid.proton.amqp.Symbol;
import org.junit.Test;
public class JMSContextIntegrationTest extends QpidJmsTestCase {
private final IntegrationTestFixture testFixture = new IntegrationTestFixture();
private Symbol[] SERVER_ANONYMOUS_RELAY = new Symbol[]{ANONYMOUS_RELAY};
@Test(timeout = 20000)
public void testCreateAndCloseContext() throws Exception {
try (TestAmqpPeer testPeer = new TestAmqpPeer();) {
JMSContext context = testFixture.createJMSContext(testPeer);
testPeer.expectClose();
context.close();
testPeer.waitForAllHandlersToComplete(1000);
}
}
@Test(timeout = 20000)
public void testCreateContextWithClientId() throws Exception {
try (TestAmqpPeer testPeer = new TestAmqpPeer();) {
JMSContext context = testFixture.createJMSContext(testPeer, false, null, null, null, true);
testPeer.expectClose();
context.close();
testPeer.waitForAllHandlersToComplete(1000);
}
}
@Test(timeout = 20000)
public void testCreateContextAndSetClientID() throws Exception {
try (TestAmqpPeer testPeer = new TestAmqpPeer();) {
JMSContext context = testFixture.createJMSContext(testPeer, false, null, null, null, false);
context.setClientID(UUID.randomUUID().toString());
testPeer.expectClose();
context.close();
testPeer.waitForAllHandlersToComplete(1000);
}
}
@Test(timeout = 20000)
public void testCreateAutoAckSessionByDefault() throws Exception {
try (TestAmqpPeer testPeer = new TestAmqpPeer();) {
JMSContext context = testFixture.createJMSContext(testPeer);
assertEquals(JMSContext.AUTO_ACKNOWLEDGE, context.getSessionMode());
testPeer.expectBegin();
context.createTopic("TopicName");
testPeer.expectEnd();
testPeer.expectClose();
context.close();
testPeer.waitForAllHandlersToComplete(1000);
}
}
@Test(timeout = 20000)
public void testCreateContextWithTransactedSessionMode() throws Exception {
Binary txnId = new Binary(new byte[]{ (byte) 5, (byte) 6, (byte) 7, (byte) 8});
try (TestAmqpPeer testPeer = new TestAmqpPeer();) {
JMSContext context = testFixture.createJMSContext(testPeer, JMSContext.SESSION_TRANSACTED);
assertEquals(JMSContext.SESSION_TRANSACTED, context.getSessionMode());
// Session should be created and a coordinator should be attached since this
// should be a TX session, then a new TX is declared, once closed the TX should
// be discharged as a roll back.
testPeer.expectBegin();
testPeer.expectCoordinatorAttach();
testPeer.expectDeclare(txnId);
testPeer.expectDischarge(txnId, true);
testPeer.expectEnd();
testPeer.expectClose();
context.createTopic("TopicName");
context.close();
testPeer.waitForAllHandlersToComplete(1000);
}
}
@Test(timeout = 20000)
public void testCreateContextFromContextWithSessionsActive() throws Exception {
try (TestAmqpPeer testPeer = new TestAmqpPeer();) {
JMSContext context = testFixture.createJMSContext(testPeer);
assertEquals(JMSContext.AUTO_ACKNOWLEDGE, context.getSessionMode());
testPeer.expectBegin();
context.createTopic("TopicName");
// Create a second should not create a new session yet, once a new connection is
// create on demand then close of the second context should only close the session
JMSContext other = context.createContext(JMSContext.CLIENT_ACKNOWLEDGE);
assertEquals(JMSContext.CLIENT_ACKNOWLEDGE, other.getSessionMode());
testPeer.expectBegin();
testPeer.expectEnd();
other.createTopic("TopicName");
other.close();
testPeer.waitForAllHandlersToComplete(1000);
// Now the connection should close down.
testPeer.expectEnd();
testPeer.expectClose();
context.close();
testPeer.waitForAllHandlersToComplete(1000);
}
}
@Test(timeout = 20000)
public void testOnlyOneProducerCreatedInSingleContext() throws Exception {
try (TestAmqpPeer testPeer = new TestAmqpPeer();) {
JMSContext context = testFixture.createJMSContext(testPeer, SERVER_ANONYMOUS_RELAY);
assertEquals(JMSContext.AUTO_ACKNOWLEDGE, context.getSessionMode());
testPeer.expectBegin();
testPeer.expectSenderAttach();
// One producer created should send an attach.
JMSProducer producer1 = context.createProducer();
assertNotNull(producer1);
// An additional one should not result in an attach
JMSProducer producer2 = context.createProducer();
assertNotNull(producer2);
testPeer.expectEnd();
testPeer.expectClose();
context.close();
testPeer.waitForAllHandlersToComplete(1000);
}
}
@Test(timeout = 20000)
public void testEachContextGetsItsOwnProducer() throws Exception {
try (TestAmqpPeer testPeer = new TestAmqpPeer();) {
JMSContext context = testFixture.createJMSContext(testPeer, SERVER_ANONYMOUS_RELAY);
assertEquals(JMSContext.AUTO_ACKNOWLEDGE, context.getSessionMode());
testPeer.expectBegin();
testPeer.expectSenderAttach();
testPeer.expectBegin();
testPeer.expectSenderAttach();
// One producer created should send an attach.
JMSProducer producer1 = context.createProducer();
assertNotNull(producer1);
// An additional one should not result in an attach
JMSContext other = context.createContext(JMSContext.AUTO_ACKNOWLEDGE);
JMSProducer producer2 = other.createProducer();
assertNotNull(producer2);
testPeer.expectEnd();
testPeer.expectEnd();
testPeer.expectClose();
other.close();
context.close();
testPeer.waitForAllHandlersToComplete(1000);
}
}
}
| apache/qpid-jms | qpid-jms-client/src/test/java/org/apache/qpid/jms/integration/JMSContextIntegrationTest.java | Java | apache-2.0 | 7,660 |
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.component.elasticsearch;
import java.util.HashMap;
import java.util.Map;
import org.apache.camel.Component;
import org.apache.camel.component.extension.ComponentVerifierExtension;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Assert;
import org.junit.Test;
public class ElasticsearchRestComponentVerifierExtensionTest extends CamelTestSupport {
// *************************************************
// Tests (parameters)
// *************************************************
@Override
public boolean isUseRouteBuilder() {
return false;
}
@Test
public void testParameters() throws Exception {
Component component = context().getComponent("elasticsearch-rest");
ComponentVerifierExtension verifier = component.getExtension(ComponentVerifierExtension.class).orElseThrow(IllegalStateException::new);
Map<String, Object> parameters = new HashMap<>();
parameters.put("hostAddresses", "http://localhost:9000");
parameters.put("clusterName", "es-test");
ComponentVerifierExtension.Result result = verifier.verify(ComponentVerifierExtension.Scope.PARAMETERS, parameters);
Assert.assertEquals(ComponentVerifierExtension.Result.Status.OK, result.getStatus());
}
@Test
public void testConnectivity() throws Exception {
Component component = context().getComponent("elasticsearch-rest");
ComponentVerifierExtension verifier = component.getExtension(ComponentVerifierExtension.class).orElseThrow(IllegalStateException::new);
Map<String, Object> parameters = new HashMap<>();
parameters.put("hostAddresses", "http://localhost:9000");
ComponentVerifierExtension.Result result = verifier.verify(ComponentVerifierExtension.Scope.CONNECTIVITY, parameters);
Assert.assertEquals(ComponentVerifierExtension.Result.Status.ERROR, result.getStatus());
}
}
| objectiser/camel | components/camel-elasticsearch-rest/src/test/java/org/apache/camel/component/elasticsearch/ElasticsearchRestComponentVerifierExtensionTest.java | Java | apache-2.0 | 2,756 |
package org.apereo.cas.authentication;
import com.google.common.base.Splitter;
import org.apereo.cas.authentication.principal.Principal;
import org.apereo.cas.services.MultifactorAuthenticationProvider;
import org.apereo.cas.services.RegisteredService;
import org.apereo.cas.services.RegisteredServiceMultifactorPolicy;
import org.apereo.cas.util.CollectionUtils;
import org.springframework.util.StringUtils;
import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
/**
* Default MFA Trigger selection strategy. This strategy looks for valid triggers in the following order: request
* parameter, RegisteredService policy, principal attribute.
*
* @author Daniel Frett
* @since 5.0.0
*/
public class DefaultMultifactorTriggerSelectionStrategy implements MultifactorTriggerSelectionStrategy {
private static final Splitter ATTR_NAMES = Splitter.on(',').trimResults().omitEmptyStrings();
private final String requestParameter;
private final String globalPrincipalAttributeNameTriggers;
public DefaultMultifactorTriggerSelectionStrategy(final String attributeNameTriggers, final String requestParameter) {
this.globalPrincipalAttributeNameTriggers = attributeNameTriggers;
this.requestParameter = requestParameter;
}
@Override
public Optional<String> resolve(final Collection<MultifactorAuthenticationProvider> providers,
final HttpServletRequest request, final RegisteredService service, final Principal principal) {
Optional<String> provider = Optional.empty();
// short-circuit if we don't have any available MFA providers
if (providers == null || providers.isEmpty()) {
return provider;
}
final Set<String> validProviderIds = providers.stream()
.map(MultifactorAuthenticationProvider::getId)
.collect(Collectors.toSet());
// check for an opt-in provider id parameter trigger, we only care about the first value
if (!provider.isPresent() && request != null) {
provider = Optional.ofNullable(request.getParameter(requestParameter))
.filter(validProviderIds::contains);
}
// check for a RegisteredService configured trigger
if (!provider.isPresent() && service != null) {
final RegisteredServiceMultifactorPolicy policy = service.getMultifactorPolicy();
if (shouldApplyRegisteredServiceMultifactorPolicy(policy, principal)) {
provider = policy.getMultifactorAuthenticationProviders().stream()
.filter(validProviderIds::contains)
.findFirst();
}
}
// check for principal attribute trigger
if (!provider.isPresent() && principal != null
&& StringUtils.hasText(globalPrincipalAttributeNameTriggers)) {
provider = StreamSupport.stream(ATTR_NAMES.split(globalPrincipalAttributeNameTriggers).spliterator(), false)
// principal.getAttribute(name).values
.map(principal.getAttributes()::get).filter(Objects::nonNull)
.map(CollectionUtils::toCollection).flatMap(Set::stream)
// validProviderIds.contains((String) value)
.filter(String.class::isInstance).map(String.class::cast).filter(validProviderIds::contains)
.findFirst();
}
// return the resolved trigger
return provider;
}
private static boolean shouldApplyRegisteredServiceMultifactorPolicy(final RegisteredServiceMultifactorPolicy policy, final Principal principal) {
final String attrName = policy.getPrincipalAttributeNameTrigger();
final String attrValue = policy.getPrincipalAttributeValueToMatch();
// Principal attribute name and/or value is not defined
if (!StringUtils.hasText(attrName) || !StringUtils.hasText(attrValue)) {
return true;
}
// no Principal, we should enforce policy
if (principal == null) {
return true;
}
// check to see if any of the specified attributes match the attrValue pattern
final Predicate<String> attrValuePredicate = Pattern.compile(attrValue).asPredicate();
return StreamSupport.stream(ATTR_NAMES.split(attrName).spliterator(), false)
.map(principal.getAttributes()::get)
.filter(Objects::nonNull)
.map(CollectionUtils::toCollection)
.flatMap(Set::stream)
.filter(String.class::isInstance)
.map(String.class::cast)
.anyMatch(attrValuePredicate);
}
}
| creamer/cas | core/cas-server-core-services/src/main/java/org/apereo/cas/authentication/DefaultMultifactorTriggerSelectionStrategy.java | Java | apache-2.0 | 4,960 |
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.jmeter.gui;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.HashSet;
import java.util.Set;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.MenuElement;
import org.apache.jmeter.exceptions.IllegalUserActionException;
import org.apache.jmeter.gui.action.AbstractAction;
import org.apache.jmeter.gui.action.ActionNames;
import org.apache.jmeter.gui.action.ActionRouter;
import org.apache.jmeter.gui.plugin.MenuCreator;
import org.apache.jmeter.util.JMeterUtils;
public class HtmlReportAction extends AbstractAction implements MenuCreator {
private static Set<String> commands = new HashSet<>();
private HtmlReportUI htmlReportPanel;
static {
commands.add(ActionNames.HTML_REPORT);
}
public HtmlReportAction() {
super();
}
@Override
public void doAction(ActionEvent e) throws IllegalUserActionException {
htmlReportPanel = new HtmlReportUI();
htmlReportPanel.showInputDialog(getParentFrame(e));
}
@Override
public Set<String> getActionNames() {
return commands;
}
@Override
public JMenuItem[] getMenuItemsAtLocation(MENU_LOCATION location) {
if (location != MENU_LOCATION.TOOLS) {
return new JMenuItem[0];
}
// Use the action name as resource key because the action name is used by JMeterMenuBar too when changing languages.
JMenuItem menuItem = new JMenuItem(JMeterUtils.getResString(ActionNames.HTML_REPORT), KeyEvent.VK_UNDEFINED);
menuItem.setName(ActionNames.HTML_REPORT);
menuItem.setActionCommand(ActionNames.HTML_REPORT);
menuItem.setAccelerator(null);
menuItem.addActionListener(ActionRouter.getInstance());
return new JMenuItem[] { menuItem };
}
@Override
public JMenu[] getTopLevelMenus() {
return new JMenu[0];
}
@Override
public boolean localeChanged(MenuElement menu) {
return false;
}
@Override
public void localeChanged() {
// NOOP
}
public HtmlReportUI getHtmlReportPanel() {
return htmlReportPanel;
}
}
| apache/jmeter | src/core/src/main/java/org/apache/jmeter/gui/HtmlReportAction.java | Java | apache-2.0 | 2,972 |
/*
* Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH
* under one or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information regarding copyright
* ownership. Camunda licenses this file to you under the Apache License,
* Version 2.0; you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.camunda.bpm.model.bpmn.impl.instance.camunda;
import static org.camunda.bpm.model.bpmn.impl.BpmnModelConstants.CAMUNDA_ELEMENT_CONNECTOR;
import static org.camunda.bpm.model.bpmn.impl.BpmnModelConstants.CAMUNDA_NS;
import org.camunda.bpm.model.bpmn.impl.instance.BpmnModelElementInstanceImpl;
import org.camunda.bpm.model.bpmn.instance.camunda.CamundaConnector;
import org.camunda.bpm.model.bpmn.instance.camunda.CamundaConnectorId;
import org.camunda.bpm.model.bpmn.instance.camunda.CamundaInputOutput;
import org.camunda.bpm.model.xml.ModelBuilder;
import org.camunda.bpm.model.xml.impl.instance.ModelTypeInstanceContext;
import org.camunda.bpm.model.xml.type.ModelElementTypeBuilder;
import org.camunda.bpm.model.xml.type.ModelElementTypeBuilder.ModelTypeInstanceProvider;
import org.camunda.bpm.model.xml.type.child.ChildElement;
import org.camunda.bpm.model.xml.type.child.SequenceBuilder;
/**
* The BPMN connector camunda extension element
*
* @author Sebastian Menski
*/
public class CamundaConnectorImpl extends BpmnModelElementInstanceImpl implements CamundaConnector {
protected static ChildElement<CamundaConnectorId> camundaConnectorIdChild;
protected static ChildElement<CamundaInputOutput> camundaInputOutputChild;
public static void registerType(ModelBuilder modelBuilder) {
ModelElementTypeBuilder typeBuilder = modelBuilder.defineType(CamundaConnector.class, CAMUNDA_ELEMENT_CONNECTOR)
.namespaceUri(CAMUNDA_NS)
.instanceProvider(new ModelTypeInstanceProvider<CamundaConnector>() {
public CamundaConnector newInstance(ModelTypeInstanceContext instanceContext) {
return new CamundaConnectorImpl(instanceContext);
}
});
SequenceBuilder sequenceBuilder = typeBuilder.sequence();
camundaConnectorIdChild = sequenceBuilder.element(CamundaConnectorId.class)
.required()
.build();
camundaInputOutputChild = sequenceBuilder.element(CamundaInputOutput.class)
.build();
typeBuilder.build();
}
public CamundaConnectorImpl(ModelTypeInstanceContext instanceContext) {
super(instanceContext);
}
public CamundaConnectorId getCamundaConnectorId() {
return camundaConnectorIdChild.getChild(this);
}
public void setCamundaConnectorId(CamundaConnectorId camundaConnectorId) {
camundaConnectorIdChild.setChild(this, camundaConnectorId);
}
public CamundaInputOutput getCamundaInputOutput() {
return camundaInputOutputChild.getChild(this);
}
public void setCamundaInputOutput(CamundaInputOutput camundaInputOutput) {
camundaInputOutputChild.setChild(this, camundaInputOutput);
}
}
| camunda/camunda-bpmn-model | src/main/java/org/camunda/bpm/model/bpmn/impl/instance/camunda/CamundaConnectorImpl.java | Java | apache-2.0 | 3,445 |
/*
* Copyright 2000-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jetbrains.edu.learning.stepic;
import com.intellij.openapi.application.Application;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.extensions.Extensions;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.startup.StartupActivity;
import com.jetbrains.edu.learning.courseGeneration.StudyProjectGenerator;
import org.jetbrains.annotations.NotNull;
import java.util.List;
public class StudyCoursesUpdater implements StartupActivity {
public static StudyCoursesUpdater getInstance() {
final StartupActivity[] extensions = Extensions.getExtensions(StartupActivity.POST_STARTUP_ACTIVITY);
for (StartupActivity extension : extensions) {
if (extension instanceof StudyCoursesUpdater) {
return (StudyCoursesUpdater) extension;
}
}
throw new UnsupportedOperationException("could not find self");
}
@Override
public void runActivity(@NotNull final Project project) {
final Application application = ApplicationManager.getApplication();
if (application.isUnitTestMode()) {
return;
}
if (checkNeeded()) {
application.executeOnPooledThread(new Runnable() {
@Override
public void run() {
final List<CourseInfo> courses = EduStepicConnector.getCourses();
StudyProjectGenerator.flushCache(courses);
}
});
}
}
public static boolean checkNeeded() {
final List<CourseInfo> courses = StudyProjectGenerator.getCoursesFromCache();
return courses.isEmpty();
}
}
| Soya93/Extract-Refactoring | python/educational-core/student/src/com/jetbrains/edu/learning/stepic/StudyCoursesUpdater.java | Java | apache-2.0 | 2,161 |
/* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.camunda.bpm.engine.test.api.runtime.util;
import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.delegate.ExecutionListener;
/**
* @author: Johannes Heinemann
*/
public class IncrementCounterListener implements ExecutionListener {
public static int counter = 0;
@Override
public void notify(DelegateExecution execution) throws Exception {
counter++;
}
}
| AlexMinsk/camunda-bpm-platform | engine/src/test/java/org/camunda/bpm/engine/test/api/runtime/util/IncrementCounterListener.java | Java | apache-2.0 | 977 |
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.deltaspike.test.api.config;
import org.apache.deltaspike.core.api.config.ConfigResolver;
import org.junit.Assert;
import org.junit.Test;
import java.util.List;
public class ConfigResolverTest
{
@Test
public void testOverruledValue()
{
String result = ConfigResolver.getPropertyValue("test");
Assert.assertEquals("test2", result);
}
@Test
public void testOrderOfAllValues()
{
List<String> result = ConfigResolver.getAllPropertyValues("test");
Assert.assertEquals(2, result.size());
Assert.assertEquals("test1", result.get(0));
Assert.assertEquals("test2", result.get(1));
}
}
| sbryzak/DeltaSpike | deltaspike/core/api/src/test/java/org/apache/deltaspike/test/api/config/ConfigResolverTest.java | Java | apache-2.0 | 1,482 |
package foo;
public class ControllerB2 extends grails.TopB {
public void foo() {
super.foo();
System.out.println("ControllerB.foo() running again!");
}
}
| spring-projects/spring-loaded | testdata/src/main/java/foo/ControllerB2.java | Java | apache-2.0 | 162 |
/*
* JBoss, Home of Professional Open Source.
* Copyright 2014 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.undertow.conduits;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.concurrent.TimeUnit;
import io.undertow.UndertowLogger;
import io.undertow.UndertowMessages;
import io.undertow.UndertowOptions;
import io.undertow.server.OpenListener;
import io.undertow.util.WorkerUtils;
import org.xnio.ChannelListener;
import org.xnio.ChannelListeners;
import org.xnio.IoUtils;
import org.xnio.Options;
import org.xnio.StreamConnection;
import org.xnio.XnioExecutor;
import org.xnio.channels.ReadTimeoutException;
import org.xnio.channels.StreamSinkChannel;
import org.xnio.conduits.AbstractStreamSourceConduit;
import org.xnio.conduits.ConduitStreamSourceChannel;
import org.xnio.conduits.ReadReadyHandler;
import org.xnio.conduits.StreamSourceConduit;
/**
* Wrapper for read timeout. This should always be the first wrapper applied to the underlying channel.
*
* @author Stuart Douglas
* @see org.xnio.Options#READ_TIMEOUT
*/
public final class ReadTimeoutStreamSourceConduit extends AbstractStreamSourceConduit<StreamSourceConduit> {
private XnioExecutor.Key handle;
private final StreamConnection connection;
private volatile long expireTime = -1;
private final OpenListener openListener;
private static final int FUZZ_FACTOR = 50; //we add 50ms to the timeout to make sure the underlying channel has actually timed out
private volatile boolean expired;
private final Runnable timeoutCommand = new Runnable() {
@Override
public void run() {
handle = null;
if (expireTime == -1) {
return;
}
long current = System.currentTimeMillis();
if (current < expireTime) {
//timeout has been bumped, re-schedule
handle = WorkerUtils.executeAfter(connection.getIoThread(),timeoutCommand, (expireTime - current) + FUZZ_FACTOR, TimeUnit.MILLISECONDS);
return;
}
UndertowLogger.REQUEST_LOGGER.tracef("Timing out channel %s due to inactivity", connection.getSourceChannel());
synchronized (ReadTimeoutStreamSourceConduit.this) {
expired = true;
}
boolean readResumed = connection.getSourceChannel().isReadResumed();
ChannelListener<? super ConduitStreamSourceChannel> readListener = connection.getSourceChannel().getReadListener();
if (readResumed) {
ChannelListeners.invokeChannelListener(connection.getSourceChannel(), readListener);
}
if (connection.getSinkChannel().isWriteResumed()) {
ChannelListeners.invokeChannelListener(connection.getSinkChannel(), connection.getSinkChannel().getWriteListener());
}
// close only after invoking listeners, to allow space for listener getting ReadTimeoutException
IoUtils.safeClose(connection);
}
};
public ReadTimeoutStreamSourceConduit(final StreamSourceConduit delegate, StreamConnection connection, OpenListener openListener) {
super(delegate);
this.connection = connection;
this.openListener = openListener;
final ReadReadyHandler handler = new ReadReadyHandler.ChannelListenerHandler<>(connection.getSourceChannel());
delegate.setReadReadyHandler(new ReadReadyHandler() {
@Override
public void readReady() {
handler.readReady();
}
@Override
public void forceTermination() {
cleanup();
handler.forceTermination();
}
@Override
public void terminated() {
cleanup();
handler.terminated();
}
});
}
private void handleReadTimeout(final long ret) throws IOException {
if (!connection.isOpen()) {
cleanup();
return;
}
if (ret == -1) {
cleanup();
return;
}
Integer timeout = getTimeout();
if (timeout == null || timeout <= 0) {
return;
}
final long currentTime = System.currentTimeMillis();
if (ret == 0) {
final long expireTimeVar = expireTime;
if (expireTimeVar != -1 && currentTime > expireTimeVar) {
IoUtils.safeClose(connection);
throw UndertowMessages.MESSAGES.readTimedOut(this.getTimeout());
}
}
expireTime = currentTime + timeout;
if (handle == null) {
handle = connection.getIoThread().executeAfter(timeoutCommand, timeout, TimeUnit.MILLISECONDS);
}
}
@Override
public long transferTo(final long position, final long count, final FileChannel target) throws IOException {
checkExpired();
long ret = super.transferTo(position, count, target);
handleReadTimeout(ret);
return ret;
}
@Override
public long transferTo(final long count, final ByteBuffer throughBuffer, final StreamSinkChannel target) throws IOException {
checkExpired();
long ret = super.transferTo(count, throughBuffer, target);
handleReadTimeout(ret);
return ret;
}
@Override
public long read(final ByteBuffer[] dsts, final int offset, final int length) throws IOException {
checkExpired();
long ret = super.read(dsts, offset, length);
handleReadTimeout(ret);
return ret;
}
@Override
public int read(final ByteBuffer dst) throws IOException {
checkExpired();
int ret = super.read(dst);
handleReadTimeout(ret);
return ret;
}
@Override
public void awaitReadable() throws IOException {
checkExpired();
Integer timeout = getTimeout();
if (timeout != null && timeout > 0) {
super.awaitReadable(timeout + FUZZ_FACTOR, TimeUnit.MILLISECONDS);
} else {
super.awaitReadable();
}
}
@Override
public void awaitReadable(long time, TimeUnit timeUnit) throws IOException {
checkExpired();
Integer timeout = getTimeout();
if (timeout != null && timeout > 0) {
long millis = timeUnit.toMillis(time);
super.awaitReadable(Math.min(millis, timeout + FUZZ_FACTOR), TimeUnit.MILLISECONDS);
} else {
super.awaitReadable(time, timeUnit);
}
}
private Integer getTimeout() {
Integer timeout = 0;
try {
timeout = connection.getSourceChannel().getOption(Options.READ_TIMEOUT);
} catch (IOException ignore) {
// should never happen
}
Integer idleTimeout = openListener.getUndertowOptions().get(UndertowOptions.IDLE_TIMEOUT);
if ((timeout == null || timeout <= 0) && idleTimeout != null) {
timeout = idleTimeout;
} else if (timeout != null && idleTimeout != null && idleTimeout > 0) {
timeout = Math.min(timeout, idleTimeout);
}
return timeout;
}
@Override
public void terminateReads() throws IOException {
checkExpired();
super.terminateReads();
cleanup();
}
private void cleanup() {
if (handle != null) {
handle.remove();
handle = null;
expireTime = -1;
}
}
@Override
public void suspendReads() {
super.suspendReads();
cleanup();
}
private void checkExpired() throws ReadTimeoutException {
synchronized (this) {
if (expired) {
throw UndertowMessages.MESSAGES.readTimedOut(System.currentTimeMillis());
}
}
}
public String toString() {
return super.toString() + " (next: " + next + ")";
}
}
| rhusar/undertow | core/src/main/java/io/undertow/conduits/ReadTimeoutStreamSourceConduit.java | Java | apache-2.0 | 8,603 |
/*
* Copyright 2021 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jbpm.test.functional.gateway;
import java.util.HashMap;
import java.util.Map;
import org.jbpm.executor.ExecutorServiceFactory;
import org.jbpm.test.JbpmTestCase;
import org.jbpm.test.wih.ListWorkItemHandler;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.kie.api.executor.ExecutorService;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.process.ProcessInstance;
import static org.junit.Assert.assertNull;
/**
* Parallel gateway execution test. 2x parallel fork, 1x join
*/
public class ParallelGatewayAsyncTest extends JbpmTestCase {
private static final String PARALLEL_GATEWAY_ASYNC = "org/jbpm/test/functional/gateway/ParallelGatewayAsync.bpmn";
private static final String PARALLEL_GATEWAY_ASYNC_ID = "org.jbpm.test.functional.gateway.ParallelGatewayAsync";
private ExecutorService executorService;
private KieSession kieSession;
private ListWorkItemHandler wih;
public ParallelGatewayAsyncTest() {
super(true, true);
}
@Override
@Before
public void setUp() throws Exception {
super.setUp();
executorService = ExecutorServiceFactory.newExecutorService(getEmf());
executorService.setInterval(1);
executorService.init();
addEnvironmentEntry("AsyncMode", "true");
addEnvironmentEntry("ExecutorService", executorService);
wih = new ListWorkItemHandler();
addWorkItemHandler("Human Task", wih);
kieSession = createKSession(PARALLEL_GATEWAY_ASYNC);
}
@After
public void tearDown() throws Exception {
executorService.clearAllErrors();
executorService.clearAllRequests();
executorService.destroy();
super.tearDown();
}
/**
* Simple parallel gateway test.
*/
@Test(timeout = 30000)
public void testParallelGatewayAsync() throws Exception {
Map<String, Object> inputs = new HashMap<>();
inputs.put("useHT", Boolean.TRUE);
inputs.put("mode", "1");
ProcessInstance pi = kieSession.startProcess(PARALLEL_GATEWAY_ASYNC_ID, inputs);
Thread.sleep(3000L);
wih.getWorkItems().forEach(e -> kieSession.getWorkItemManager().completeWorkItem(e.getId(), e.getParameters()));
Thread.sleep(1000L);
assertNull(kieSession.getProcessInstance(pi.getId()));
}
}
| droolsjbpm/jbpm | jbpm-test-coverage/src/test/java/org/jbpm/test/functional/gateway/ParallelGatewayAsyncTest.java | Java | apache-2.0 | 3,008 |
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.waveprotocol.wave.client.editor.content.paragraph;
import static org.waveprotocol.wave.client.editor.Editor.ROOT_HANDLER_REGISTRY;
import com.google.gwt.dom.client.Element;
import junit.framework.TestCase;
import org.waveprotocol.wave.client.editor.Editor;
import org.waveprotocol.wave.client.editor.EditorTestingUtil;
import org.waveprotocol.wave.client.editor.content.CMutableDocument;
import org.waveprotocol.wave.client.editor.content.ContentDocElement;
import org.waveprotocol.wave.client.editor.content.ContentDocument;
import org.waveprotocol.wave.client.editor.content.ContentDocument.PermanentMutationHandler;
import org.waveprotocol.wave.client.editor.content.ContentElement;
import org.waveprotocol.wave.client.editor.content.ContentNode;
import org.waveprotocol.wave.client.editor.content.HasImplNodelets;
import org.waveprotocol.wave.client.editor.content.paragraph.OrderedListRenumberer.LevelNumbers;
import org.waveprotocol.wave.client.editor.content.paragraph.Paragraph.Alignment;
import org.waveprotocol.wave.client.editor.content.paragraph.Paragraph.Direction;
import org.waveprotocol.wave.client.scheduler.FinalTaskRunner;
import org.waveprotocol.wave.client.scheduler.Scheduler.Task;
import org.waveprotocol.wave.model.document.indexed.IndexedDocumentImpl;
import org.waveprotocol.wave.model.document.operation.Attributes;
import org.waveprotocol.wave.model.document.operation.impl.DocInitializationBuilder;
import org.waveprotocol.wave.model.document.util.Point;
import org.waveprotocol.wave.model.schema.conversation.ConversationSchemas;
import org.waveprotocol.wave.model.util.CollectionUtils;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
/**
* Utilities for testing ordered list numbering.
*
* A bunch of methods refer to lines by "index". This is index into the
* conceptual list of lines, so, 0 for the first line, 1 for the second line,
* and so forth.
*
* @author danilatos@google.com (Daniel Danilatos)
*/
public abstract class RenumbererTestBase extends TestCase {
/**
* Simple enum for representing a style of line, that maps to the type and
* li-style type attributes. Contains a representative sample of the types of
* lines that could possibly have different effects on renumbering.
*/
enum Type {
/** No attributes */
NONE,
/** t=h1 */
HEADING,
/** t=li without listyle */
LIST,
/** t=li with listyle = decimal */
DECIMAL // DECIMAL must come last
}
/**
* Fake renderer that doesn't depend on any DOM stuff.
*/
ParagraphHtmlRenderer renderer = new ParagraphHtmlRenderer() {
@Override
public Element createDomImpl(Renderable element) {
return null;
}
@Override
public void updateRendering(HasImplNodelets element, String type, String listStyle, int indent,
Alignment alignment, Direction direction) {
}
@Override
public void updateListValue(HasImplNodelets element, int value) {
assertEquals(Line.fromParagraph(((ContentElement) element)).getCachedNumberValue(), value);
}
};
/**
* Renumberer being tested.
*/
final OrderedListRenumberer renumberer = new OrderedListRenumberer(renderer);
/**
* Batch render task that will get scheduled.
*/
Task scheduledTask;
/**
* Simple fake take runner that just sets {@link #scheduledTask}
*/
final FinalTaskRunner runner = new FinalTaskRunner() {
@Override public void scheduleFinally(Task task) {
assertTrue(scheduledTask == null || scheduledTask == task);
scheduledTask = task;
}
};
/**
* Same as a regular ParagraphRenderer but tagged with
* {@link PermanentMutationHandler} so that it gets used even in POJO document mode.
*/
static class Renderer extends ParagraphRenderer implements PermanentMutationHandler {
Renderer(ParagraphHtmlRenderer htmlRenderer, OrderedListRenumberer renumberer,
FinalTaskRunner finalRaskRunner) {
super(htmlRenderer, renumberer, finalRaskRunner);
// TODO Auto-generated constructor stub
}
}
ContentDocument content1;
ContentDocument content2;
CMutableDocument doc1;
CMutableDocument doc2;
/**
* Current doc being used. For some tests we render more than one doc to test
* the sharing of a single renumberer between multiple documents.
*/
CMutableDocument doc;
/** Number of lines in test documents */
final int SIZE = 10;
@Override
protected void setUp() {
EditorTestingUtil.setupTestEnvironment();
ContentDocElement.register(ROOT_HANDLER_REGISTRY, ContentDocElement.DEFAULT_TAGNAME);
Paragraph.register(ROOT_HANDLER_REGISTRY);
LineRendering.registerLines(ROOT_HANDLER_REGISTRY);
LineRendering.registerParagraphRenderer(Editor.ROOT_HANDLER_REGISTRY,
new Renderer(renderer, renumberer, runner));
renumberer.updateHtmlEvenWhenNullImplNodelet = true;
DocInitializationBuilder builder = new DocInitializationBuilder();
builder.elementStart("body", Attributes.EMPTY_MAP);
for (int i = 0; i < SIZE; i++) {
builder.elementStart("line", Attributes.EMPTY_MAP).elementEnd();
}
builder.elementEnd();
content1 = new ContentDocument(ConversationSchemas.BLIP_SCHEMA_CONSTRAINTS);
content1.setRegistries(Editor.ROOT_REGISTRIES);
content1.consume(builder.build());
doc1 = content1.getMutableDoc();
content2 = new ContentDocument(ConversationSchemas.BLIP_SCHEMA_CONSTRAINTS);
content2.setRegistries(Editor.ROOT_REGISTRIES);
content2.consume(builder.build());
doc2 = content2.getMutableDoc();
doc = doc1;
runTask();
}
/**
* Performs a randomized test of renumbering logic.
*
* @param testIterations number of test iterations on the same document. Each
* iteration does a substantial amount of work (depending on document
* size).
* @param seed initial random seed.
*/
void doRandomTest(int testIterations, int seed) {
ContentDocument.performExpensiveChecks = false;
ContentDocument.validateLocalOps = false;
IndexedDocumentImpl.performValidation = false;
final int LEVELS = 4;
final int MAX_RUN = 3;
final int ITERS_PER_BATCH_RENDER = 6;
final int DECIMALS_TO_OTHERS = 4; // ratio of decimal bullets to other stuff
final int UPDATE_TO_ADD_REMOVE = 4; // ratio of updates to node adds/removals
assertNull(scheduledTask);
int maxRand = 5;
Random r = new Random(seed);
// For each iteration
for (int iter = 0; iter < testIterations; iter++) {
info("Iter: " + iter);
// Repeat several times for a single batch render, to make sure we are
// able to handle multiple overlapping, redundant updates.
// Times two because we are alternating between two documents to test
// the ability of the renumberer to handle more than one document
// correctly.
int innerIters = (r.nextInt(ITERS_PER_BATCH_RENDER) + 1) * 2;
for (int inner = 0; inner < innerIters; inner++) {
doc = doc1; // (inner % 2 == 0) ? doc1 : doc2;
int totalLines = (doc.size() - 2) / 2;
Line line = getFirstLine();
// Pick a random section of the document to perform a bunch of random
// changes to
int i = 0;
int a = r.nextInt(totalLines);
int b = r.nextInt(totalLines);
int startSection = Math.min(a, b);
int endSection = Math.max(a, b);
while (i < startSection) {
i++;
line = line.next();
}
while (i < endSection && line != null) {
// Pick a random indentation to set
int level = r.nextInt(LEVELS);
// Length of run of elements to update
int length;
// Whether we are making them numbered items or doing something else
boolean decimal;
if (r.nextInt(DECIMALS_TO_OTHERS) == 0) {
// No need making it a long run for non-numbered items.
length = r.nextInt(2);
decimal = false;
} else {
decimal = true;
length = r.nextInt(MAX_RUN - 1) + 1;
}
while (length > 0 && i < endSection && line != null) {
boolean fiftyFifty = i % 2 == 0;
// If we're numbering these lines, then DECIMAL, otherwise choose a
// random other type.
Type type = decimal ? Type.DECIMAL : Type.values()[r.nextInt(Type.values().length - 1)];
// Randomly decide to add/remove, or to update
if (r.nextInt(UPDATE_TO_ADD_REMOVE) == 0) {
int index = index(line);
// Randomly decide to add or remove.
// Include some constraints to ensure the document doesn't get too small or too large.
boolean add = index == 0 ||
totalLines < SIZE / 2 ? true : (totalLines > SIZE * 2 ? false : r.nextBoolean());
if (add) {
line = create(index, type, level, r.nextBoolean());
} else {
line = delete(index);
if (line == null) {
// We just deleted the last line.
continue;
}
}
assert line != null;
} else {
update(index(line), type, level, fiftyFifty);
}
length--;
i++;
line = line.next();
}
}
}
check(iter);
}
}
/**
* @return index for the given line object (0 for the first line, etc).
*/
int index(Line line) {
return (doc.getLocation(line.getLineElement()) - 1) / 2;
}
/**
* @return the line element for the given index.
*/
ContentElement getLineElement(int index) {
return doc.locate(index * 2 + 1).getNodeAfter().asElement();
}
/**
* @return the first line object
*/
Line getFirstLine() {
return Line.getFirstLineOfContainer(doc.getDocumentElement().getFirstChild().asElement());
}
/**
* Creates and returns a new line.
*
* @param createAndUpdateSeparately if true, creates a line, then sets the
* attributes as a separate operation. Otherwise, sets them all at
* once. We want to test both scenarios.
*/
Line create(int index, Type type, int indent, boolean createAndUpdateSeparately) {
// info("Creating @" + index + " " +
// type + " " + indent + " " + createAndUpdateSeparately);
Point<ContentNode> loc = doc.locate(index * 2 + 1);
Line l;
if (createAndUpdateSeparately) {
l = Line.fromLineElement(
doc.createElement(loc, "line", Attributes.EMPTY_MAP));
update(index, type, indent);
} else {
l = Line.fromLineElement(
doc.createElement(loc, "line", attributes(type, indent, false, true)));
}
assertNotNull(l);
return l;
}
/**
* Deletes the line at the specified index.
*/
Line delete(int index) {
// info("Deleting @" + index);
assert index != 0 : "Code doesn't (yet) support killing the initial line";
ContentElement e = getLineElement(index);
Line line = Line.fromLineElement(e).next();
doc.deleteNode(e);
return line;
}
/**
* Updates the attributes of the line at the specified index.
*/
void update(int index, Type type, int indent) {
update(index, type, indent, true);
}
/**
* Updates the attributes of the line at the specified index.
*
* @param alwaysSetRedundant if true, always set the listyle attribute even if it
* is not necessary. For example, if the listyle attribute was
* "decimal", but the type is "HEADING", the listyle attribute should
* normally be ignored and has no meaning. It won't make a difference
* if it is set or not. We want to test both scenarios.
*/
void update(int index, Type type, int indent, boolean alwaysSetRedundant) {
ContentElement e = getLineElement(index);
// info("Making @" + ((doc.getLocation(e) - 1)/2) + " " +
// type + " " + indent + " " + alwaysSetStyle);
Map<String, String> updates = attributes(type, indent, alwaysSetRedundant, false);
for (Map.Entry<String, String> pair : updates.entrySet()) {
doc.setElementAttribute(e, pair.getKey(), pair.getValue());
}
}
/**
* Creates the map of element attributes for the given parameters.
*
* @param alwaysSetStyle see {@link #update(int, Type, int, boolean)}
* @param noNulls eliminate keys that would have null values. We want nulls
* for updates, but no nulls for creates.
*/
Map<String, String> attributes(Type type, int indent, boolean alwaysSetStyle, boolean noNulls) {
Map<String, String> updates = new HashMap<String, String>();
String levelStr = (indent > 0 ? "" + indent : null);
maybePut(updates, Paragraph.INDENT_ATTR, levelStr, noNulls);
String t = null;
String lt = null;
switch (type) {
case HEADING: t = "h1"; break;
case LIST: t = Paragraph.LIST_TYPE; break;
case DECIMAL: t = Paragraph.LIST_TYPE; lt = Paragraph.LIST_STYLE_DECIMAL; break;
}
maybePut(updates, Paragraph.SUBTYPE_ATTR, t, noNulls);
if (alwaysSetStyle || type == Type.LIST || type == Type.DECIMAL) {
maybePut(updates, Paragraph.LIST_STYLE_ATTR, lt, noNulls);
}
return updates;
}
void maybePut(Map<String, String> map, String key, String val, boolean noNull) {
if (val != null || !noNull) {
map.put(key, val);
}
}
/**
* Check the current line numbering is consistent with the document state.
*/
void check() {
check(-1);
}
/**
* Check the current line numbering is consistent with the document state.
*
* @param iter current test iteration, for debugging/logging purposes.
*/
void check(int iter) {
runTask();
// if (iter >= 1740) {
// info("\n\nCHECKING\n");
// printInfo(null, "XX");
// info("---");
// }
LevelNumbers numbers = new LevelNumbers(0, 1);
Line line = getFirstLine();
while (line != null) {
int indent = line.getIndent();
numbers.setLevel(indent);
if (line.isDecimalListItem()) {
int num = numbers.getNumberAndIncrement();
assertFalse(line.getCachedNumberValue() == Line.DIRTY);
if (num != line.getCachedNumberValue()) {
String msg = "Expected: " + num + ", got: " + line.getCachedNumberValue();
printInfo(line, msg);
fail("Wrong number on iteration " + iter + ". " + msg +
". See stdout & stderr for debug details");
}
} else {
numbers.setNumber(1);
}
line = line.next();
}
// info("^^^");
}
void runTask() {
if (scheduledTask != null) {
scheduledTask.execute();
}
scheduledTask = null;
}
void printInfo(Line badLine, String msg) {
Line line = getFirstLine();
PrintStream stream = System.out;
int i = 0;
while (line != null) {
int indent = line.getIndent();
stream.println(
CollectionUtils.repeat('.', line.getIndent()) +
line.toString() +
" indent:" + indent +
CollectionUtils.repeat(' ', 20) + line.getCachedNumberValue() + " (" + i + ")");
if (line == badLine) {
stream.println("\n\n\n");
stream = System.err;
stream.println(msg);
stream.println(">>>>>>>>>>>>>>>>>>>>>>>>> DIED ON LINE ABOVE <<<<<<<<<<<<<<<<<<\n\n");
}
line = line.next();
i++;
}
}
void info(Object msg) {
// Uncomment for debugging
// System.out.println(msg == null ? "null" : msg.toString());
}
}
| vega113/incubator-wave | wave/src/test/java/org/waveprotocol/wave/client/editor/content/paragraph/RenumbererTestBase.java | Java | apache-2.0 | 16,472 |
/*
* Licensed to Metamarkets Group Inc. (Metamarkets) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Metamarkets licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package io.druid.server.initialization.jetty;
import com.google.inject.Binder;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.Module;
import com.google.inject.Provides;
import com.metamx.common.lifecycle.Lifecycle;
import com.metamx.common.logger.Logger;
import io.druid.guice.JsonConfigProvider;
import io.druid.guice.LazySingleton;
import io.druid.guice.LifecycleModule;
import io.druid.guice.annotations.RemoteChatHandler;
import io.druid.guice.annotations.Self;
import io.druid.server.DruidNode;
import io.druid.server.initialization.ServerConfig;
import org.eclipse.jetty.server.Server;
import java.util.Properties;
/**
*/
public class ChatHandlerServerModule implements Module
{
private static final Logger log = new Logger(ChatHandlerServerModule.class);
@Inject
private Properties properties;
@Override
public void configure(Binder binder)
{
/** If "druid.indexer.task.chathandler.port" property is set then we assume that a
* separate Jetty Server with it's own {@link ServerConfig} is required for ingestion apart from the query server
* otherwise we bind {@link DruidNode} annotated with {@link RemoteChatHandler} to {@literal @}{@link Self} {@link DruidNode}
* so that same Jetty Server is used for querying as well as ingestion
*/
if (properties.containsKey("druid.indexer.task.chathandler.port")) {
log.info("Spawning separate ingestion server at port [%s]", properties.get("druid.indexer.task.chathandler.port"));
JsonConfigProvider.bind(binder, "druid.indexer.task.chathandler", DruidNode.class, RemoteChatHandler.class);
JsonConfigProvider.bind(binder, "druid.indexer.server.chathandler.http", ServerConfig.class, RemoteChatHandler.class);
LifecycleModule.register(binder, Server.class, RemoteChatHandler.class);
} else {
binder.bind(DruidNode.class).annotatedWith(RemoteChatHandler.class).to(Key.get(DruidNode.class, Self.class));
binder.bind(ServerConfig.class).annotatedWith(RemoteChatHandler.class).to(Key.get(ServerConfig.class));
}
}
@Provides
@LazySingleton
@RemoteChatHandler
public Server getServer(Injector injector, Lifecycle lifecycle, @RemoteChatHandler DruidNode node, @RemoteChatHandler ServerConfig config)
{
final Server server = JettyServerModule.makeJettyServer(node, config);
JettyServerModule.initializeServer(injector, lifecycle, server);
return server;
}
}
| fjy/druid | server/src/main/java/io/druid/server/initialization/jetty/ChatHandlerServerModule.java | Java | apache-2.0 | 3,297 |
/*
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license
* agreements. See the NOTICE file distributed with this work for additional information regarding
* copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License. You may obtain a
* copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package org.apache.geode.cache.lucene;
import static org.apache.geode.cache.lucene.test.LuceneTestUtilities.INDEX_NAME;
import static org.apache.geode.cache.lucene.test.LuceneTestUtilities.REGION_NAME;
import static org.apache.geode.test.awaitility.GeodeAwaitility.await;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.util.List;
import java.util.stream.IntStream;
import junitparams.JUnitParamsRunner;
import junitparams.Parameters;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
import org.apache.geode.cache.Cache;
import org.apache.geode.cache.Region;
import org.apache.geode.internal.cache.GemFireCacheImpl;
import org.apache.geode.internal.cache.PartitionedRegion;
import org.apache.geode.internal.cache.control.HeapMemoryMonitor;
import org.apache.geode.test.dunit.SerializableRunnableIF;
import org.apache.geode.test.junit.categories.LuceneTest;
@Category({LuceneTest.class})
@RunWith(JUnitParamsRunner.class)
public class EvictionDUnitTest extends LuceneQueriesAccessorBase {
protected static final float INITIAL_EVICTION_HEAP_PERCENTAGE = 50.9f;
protected static final float EVICTION_HEAP_PERCENTAGE_FAKE_NOTIFICATION = 85.0f;
protected static final int TEST_MAX_MEMORY = 100;
protected static final int MEMORY_USED_FAKE_NOTIFICATION = 90;
protected RegionTestableType[] getPartitionRedundantOverflowEvictionRegionType() {
return new RegionTestableType[] {
RegionTestableType.PARTITION_PERSISTENT_REDUNDANT_EVICTION_OVERFLOW};
}
protected RegionTestableType[] getPartitionRedundantLocalDestroyEvictionRegionType() {
return new RegionTestableType[] {RegionTestableType.PARTITION_REDUNDANT_EVICTION_LOCAL_DESTROY,
RegionTestableType.PARTITION_REDUNDANT_PERSISTENT_EVICTION_LOCAL_DESTROY,
RegionTestableType.PARTITION_EVICTION_LOCAL_DESTROY,
RegionTestableType.PARTITION_PERSISTENT_EVICTION_LOCAL_DESTROY};
}
@Test
@Parameters(method = "getPartitionRedundantLocalDestroyEvictionRegionType")
public void regionWithEvictionWithLocalDestroyMustNotbeAbleToCreateLuceneIndexes(
RegionTestableType regionTestType) {
SerializableRunnableIF createIndex = getSerializableRunnableIFCreateIndex();
dataStore1.invoke(() -> {
try {
initDataStore(createIndex, regionTestType);
} catch (UnsupportedOperationException e) {
assertEquals(
"Lucene indexes on regions with eviction and action local destroy are not supported",
e.getMessage());
assertNull(getCache().getRegion(REGION_NAME));
}
});
}
private SerializableRunnableIF getSerializableRunnableIFCreateIndex() {
return () -> {
LuceneService luceneService = LuceneServiceProvider.get(getCache());
luceneService.createIndexFactory().setFields("text").create(INDEX_NAME, REGION_NAME);
};
}
@Test
@Parameters(method = "getPartitionRedundantOverflowEvictionRegionType")
public void regionsWithEvictionWithOverflowMustBeAbleToCreateLuceneIndexes(
RegionTestableType regionTestType) {
SerializableRunnableIF createIndex = () -> {
LuceneService luceneService = LuceneServiceProvider.get(getCache());
luceneService.createIndexFactory().setFields("text").create(INDEX_NAME, REGION_NAME);
};
dataStore1.invoke(() -> initDataStore(createIndex, regionTestType));
accessor.invoke(() -> initDataStore(createIndex, regionTestType));
accessor.invoke(() -> {
Cache cache = getCache();
Region region = cache.getRegion(REGION_NAME);
IntStream.range(0, NUM_BUCKETS).forEach(i -> region.put(i, new TestObject("hello world")));
});
waitForFlushBeforeExecuteTextSearch(accessor, 60000);
dataStore1.invoke(() -> {
try {
getCache().getResourceManager().setEvictionHeapPercentage(INITIAL_EVICTION_HEAP_PERCENTAGE);
final PartitionedRegion partitionedRegion = (PartitionedRegion) getRootRegion(REGION_NAME);
raiseFakeNotification();
await().untilAsserted(() -> {
assertTrue(partitionedRegion.getDiskRegionStats().getNumOverflowOnDisk() > 0);
});
} finally {
cleanUpAfterFakeNotification();
}
});
accessor.invoke(() -> {
LuceneService luceneService = LuceneServiceProvider.get(getCache());
LuceneQuery<Integer, TestObject> query = luceneService.createLuceneQueryFactory()
.setLimit(100).create(INDEX_NAME, REGION_NAME, "world", "text");
List<LuceneResultStruct<Integer, TestObject>> resultList = query.findResults();
assertEquals(NUM_BUCKETS, resultList.size());
});
}
protected void raiseFakeNotification() {
((GemFireCacheImpl) getCache()).getHeapEvictor().setTestAbortAfterLoopCount(1);
HeapMemoryMonitor.setTestDisableMemoryUpdates(true);
getCache().getResourceManager()
.setEvictionHeapPercentage(EVICTION_HEAP_PERCENTAGE_FAKE_NOTIFICATION);
HeapMemoryMonitor heapMemoryMonitor =
((GemFireCacheImpl) getCache()).getInternalResourceManager().getHeapMonitor();
heapMemoryMonitor.setTestMaxMemoryBytes(TEST_MAX_MEMORY);
heapMemoryMonitor.updateStateAndSendEvent(MEMORY_USED_FAKE_NOTIFICATION, "test");
}
protected void cleanUpAfterFakeNotification() {
((GemFireCacheImpl) getCache()).getHeapEvictor().setTestAbortAfterLoopCount(Integer.MAX_VALUE);
HeapMemoryMonitor.setTestDisableMemoryUpdates(false);
}
}
| masaki-yamakawa/geode | geode-lucene/src/distributedTest/java/org/apache/geode/cache/lucene/EvictionDUnitTest.java | Java | apache-2.0 | 6,321 |
//where
/** Generate code to create an array with given element type and number
* of dimensions.
*/
Item makeNewArray(DiagnosticPosition pos, Type type, int ndims) {
try {//我加上的
DEBUG.P(this,"makeNewArray(3)");
DEBUG.P("type="+type);
DEBUG.P("ndims="+ndims);
Type elemtype = types.elemtype(type);
if (types.dimensions(elemtype) + ndims > ClassFile.MAX_DIMENSIONS) {
log.error(pos, "limit.dimensions");
nerrs++;
}
int elemcode = Code.arraycode(elemtype);
DEBUG.P("elemcode="+elemcode);
if (elemcode == 0 || (elemcode == 1 && ndims == 1)) {
code.emitAnewarray(makeRef(pos, elemtype), type);
} else if (elemcode == 1) {
code.emitMultianewarray(ndims, makeRef(pos, type), type);
} else {
code.emitNewarray(elemcode, type);
}
return items.makeStackItem(type);
}finally{//我加上的
DEBUG.P(0,this,"makeNewArray(3)");
}
} | mashuai/Open-Source-Research | Javac2007/流程/jvm/16Gen/makeNewArray.java | Java | apache-2.0 | 914 |
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.calcite.util;
import org.apache.calcite.avatica.util.DateTimeUtils;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import java.util.Calendar;
import java.util.regex.Pattern;
import javax.annotation.Nonnull;
/**
* Time literal.
*
* <p>Immutable, internally represented as a string (in ISO format),
* and can support unlimited precision (milliseconds, nanoseconds).
*/
public class TimeString implements Comparable<TimeString> {
private static final Pattern PATTERN =
Pattern.compile("[0-9][0-9]:[0-9][0-9]:[0-9][0-9](\\.[0-9]*[1-9])?");
final String v;
/** Internal constructor, no validation. */
private TimeString(String v, @SuppressWarnings("unused") boolean ignore) {
this.v = v;
}
/** Creates a TimeString. */
public TimeString(String v) {
this(v, false);
Preconditions.checkArgument(PATTERN.matcher(v).matches(),
"Invalid time format:", v);
Preconditions.checkArgument(getHour() >= 0 && getHour() < 24,
"Hour out of range:", getHour());
Preconditions.checkArgument(getMinute() >= 0 && getMinute() < 60,
"Minute out of range:", getMinute());
Preconditions.checkArgument(getSecond() >= 0 && getSecond() < 60,
"Second out of range:", getSecond());
}
/** Creates a TimeString for hour, minute, second and millisecond values. */
public TimeString(int h, int m, int s) {
this(hms(h, m, s), false);
}
/** Validates an hour-minute-second value and converts to a string. */
private static String hms(int h, int m, int s) {
Preconditions.checkArgument(h >= 0 && h < 24, "Hour out of range:", h);
Preconditions.checkArgument(m >= 0 && m < 60, "Minute out of range:", m);
Preconditions.checkArgument(s >= 0 && s < 60, "Second out of range:", s);
final StringBuilder b = new StringBuilder();
DateTimeStringUtils.hms(b, h, m, s);
return b.toString();
}
/** Sets the fraction field of a {@code TimeString} to a given number
* of milliseconds. Nukes the value set via {@link #withNanos}.
*
* <p>For example,
* {@code new TimeString(1970, 1, 1, 2, 3, 4).withMillis(56)}
* yields {@code TIME '1970-01-01 02:03:04.056'}. */
public TimeString withMillis(int millis) {
Preconditions.checkArgument(millis >= 0 && millis < 1000);
return withFraction(DateTimeStringUtils.pad(3, millis));
}
/** Sets the fraction field of a {@code TimeString} to a given number
* of nanoseconds. Nukes the value set via {@link #withMillis(int)}.
*
* <p>For example,
* {@code new TimeString(1970, 1, 1, 2, 3, 4).withNanos(56789)}
* yields {@code TIME '1970-01-01 02:03:04.000056789'}. */
public TimeString withNanos(int nanos) {
Preconditions.checkArgument(nanos >= 0 && nanos < 1000000000);
return withFraction(DateTimeStringUtils.pad(9, nanos));
}
/** Sets the fraction field of a {@code TimeString}.
* The precision is determined by the number of leading zeros.
* Trailing zeros are stripped.
*
* <p>For example,
* {@code new TimeString(1970, 1, 1, 2, 3, 4).withFraction("00506000")}
* yields {@code TIME '1970-01-01 02:03:04.00506'}. */
public TimeString withFraction(String fraction) {
String v = this.v;
int i = v.indexOf('.');
if (i >= 0) {
v = v.substring(0, i);
}
while (fraction.endsWith("0")) {
fraction = fraction.substring(0, fraction.length() - 1);
}
if (fraction.length() > 0) {
v = v + "." + fraction;
}
return new TimeString(v);
}
@Override public String toString() {
return v;
}
@Override public boolean equals(Object o) {
// The value is in canonical form (no trailing zeros).
return o == this
|| o instanceof TimeString
&& ((TimeString) o).v.equals(v);
}
@Override public int hashCode() {
return v.hashCode();
}
@Override public int compareTo(@Nonnull TimeString o) {
return v.compareTo(o.v);
}
/** Creates a TimeString from a Calendar. */
public static TimeString fromCalendarFields(Calendar calendar) {
return new TimeString(
calendar.get(Calendar.HOUR_OF_DAY),
calendar.get(Calendar.MINUTE),
calendar.get(Calendar.SECOND))
.withMillis(calendar.get(Calendar.MILLISECOND));
}
public static TimeString fromMillisOfDay(int i) {
return new TimeString(DateTimeUtils.unixTimeToString(i))
.withMillis((int) DateTimeUtils.floorMod(i, 1000));
}
public TimeString round(int precision) {
Preconditions.checkArgument(precision >= 0);
int targetLength = 9 + precision;
if (v.length() <= targetLength) {
return this;
}
String v = this.v.substring(0, targetLength);
while (v.length() >= 9 && (v.endsWith("0") || v.endsWith("."))) {
v = v.substring(0, v.length() - 1);
}
return new TimeString(v);
}
public int getMillisOfDay() {
int h = Integer.valueOf(v.substring(0, 2));
int m = Integer.valueOf(v.substring(3, 5));
int s = Integer.valueOf(v.substring(6, 8));
int ms = getMillisInSecond();
return (int) (h * DateTimeUtils.MILLIS_PER_HOUR
+ m * DateTimeUtils.MILLIS_PER_MINUTE
+ s * DateTimeUtils.MILLIS_PER_SECOND
+ ms);
}
private int getMillisInSecond() {
switch (v.length()) {
case 8: // "12:34:56"
return 0;
case 10: // "12:34:56.7"
return Integer.valueOf(v.substring(9)) * 100;
case 11: // "12:34:56.78"
return Integer.valueOf(v.substring(9)) * 10;
case 12: // "12:34:56.789"
default: // "12:34:56.7890000012345"
return Integer.valueOf(v.substring(9, 12));
}
}
private int getHour() {
return Integer.parseInt(v.substring(0, 2));
}
private int getMinute() {
return Integer.parseInt(this.v.substring(3, 5));
}
private int getSecond() {
return Integer.parseInt(this.v.substring(6, 8));
}
public Calendar toCalendar() {
return Util.calendar(getMillisOfDay());
}
/** Converts this TimestampString to a string, truncated or padded with
* zeroes to a given precision. */
public String toString(int precision) {
Preconditions.checkArgument(precision >= 0);
final int p = precision();
if (precision < p) {
return round(precision).toString(precision);
}
if (precision > p) {
String s = v;
if (p == 0) {
s += ".";
}
return s + Strings.repeat("0", precision - p);
}
return v;
}
private int precision() {
return v.length() < 9 ? 0 : (v.length() - 9);
}
}
// End TimeString.java
| xhoong/incubator-calcite | core/src/main/java/org/apache/calcite/util/TimeString.java | Java | apache-2.0 | 7,356 |
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.sling.models.it;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import javax.jcr.Node;
import javax.jcr.Session;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.junit.annotations.SlingAnnotationsTestRunner;
import org.apache.sling.junit.annotations.TestReference;
import org.apache.sling.models.factory.ModelClassException;
import org.apache.sling.models.factory.ModelFactory;
import org.apache.sling.models.it.models.ConstructorInjectionTestModel;
import org.apache.sling.models.it.models.FieldInjectionTestModel;
import org.apache.sling.models.it.models.InterfaceInjectionTestModel;
import org.apache.sling.models.it.models.implextend.InvalidImplementsInterfacePropertyModel;
import org.apache.sling.models.it.models.implextend.SampleServiceInterface;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(SlingAnnotationsTestRunner.class)
public class ModelFactorySimpleTest {
@TestReference
private ResourceResolverFactory rrFactory;
@TestReference
private ModelFactory modelFactory;
private String value;
private ResourceResolver resolver;
private Resource resource;
private Node createdNode;
@Before
public void setUp() throws Exception {
value = RandomStringUtils.randomAlphanumeric(10);
resolver = rrFactory.getAdministrativeResourceResolver(null);
Session session = resolver.adaptTo(Session.class);
Node rootNode = session.getRootNode();
createdNode = rootNode.addNode("test_" + RandomStringUtils.randomAlphanumeric(10));
createdNode.setProperty("testProperty", value);
session.save();
resource = resolver.getResource(createdNode.getPath());
}
@After
public void tearDown() throws Exception {
if (createdNode != null) {
createdNode.remove();
}
if (resolver != null) {
resolver.close();
}
}
@Test
public void testCreateModel() {
FieldInjectionTestModel model = modelFactory.createModel(resource, FieldInjectionTestModel.class);
assertNotNull("Model is null", model);
assertEquals("Test Property is not set correctly", value, model.getTestProperty());
assertNotNull("Filters is null", model.getFilters());
assertSame("Adaptable is not injected", resource, model.getResource());
}
private static final class DummyClass {
}
@Test
public void testIsModelClass() {
assertTrue("Model is not detected as such", modelFactory.isModelClass(ConstructorInjectionTestModel.class));
assertFalse("Dummy class incorrectly detected as model class", modelFactory.isModelClass(DummyClass.class));
assertFalse("Model with invalid adaptable incorrectly detected as model class" , modelFactory.isModelClass(InvalidImplementsInterfacePropertyModel.class));
assertTrue("Model is not detected as such", modelFactory.isModelClass(SampleServiceInterface.class)); // being provided by two adapters
}
@Test
public void testCanCreateFromAdaptable() {
assertTrue("Model is not detected as such", modelFactory.canCreateFromAdaptable(resource, ConstructorInjectionTestModel.class));
assertTrue("Model is not detected as such", modelFactory.canCreateFromAdaptable(resource, SampleServiceInterface.class));
assertFalse("Model is not detected as such", modelFactory.canCreateFromAdaptable(new String(), ConstructorInjectionTestModel.class)); // invalid adaptable
}
@Test(expected=ModelClassException.class)
public void testCanCreateFromAdaptableWithModelExceptin() {
modelFactory.canCreateFromAdaptable(resource, DummyClass.class); // no model class
}
}
| nleite/sling | bundles/extensions/models/integration-tests/src/main/java/org/apache/sling/models/it/ModelFactorySimpleTest.java | Java | apache-2.0 | 4,933 |
/**
* <copyright>
* Copyright (c) 2008 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
* </copyright>
*/
package org.eclipse.bpel.ui.adapters;
import java.util.List;
import org.eclipse.bpel.model.BPELPackage;
import org.eclipse.bpel.model.MessageExchanges;
import org.eclipse.bpel.ui.BPELUIPlugin;
import org.eclipse.bpel.ui.IBPELUIConstants;
import org.eclipse.bpel.ui.adapters.delegates.ReferenceContainer;
import org.eclipse.bpel.ui.editparts.MessageExchangesEditPart;
import org.eclipse.bpel.ui.editparts.OutlineTreeEditPart;
import org.eclipse.bpel.ui.properties.PropertiesLabelProvider;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPartFactory;
import org.eclipse.swt.graphics.Image;
import org.eclipse.bpel.ui.Messages;
/**
*
* @author Miriam Grundig (MGrundig@de.ibm.com)
*/
public class MessageExchangesAdapter extends ContainerAdapter implements EditPartFactory,
ILabeledElement, IOutlineEditPartFactory, ITrayEditPartFactory
{
public MessageExchangesAdapter() {
super();
}
/* IContainer delegate */
public IContainer createContainerDelegate() {
return new ReferenceContainer(BPELPackage.eINSTANCE.getMessageExchanges_Children());
}
/* EditPartFactory */
public EditPart createEditPart(EditPart context, Object model) {
MessageExchangesEditPart result = new MessageExchangesEditPart();
result.setLabelProvider(PropertiesLabelProvider.getInstance());
result.setModel(model);
return result;
}
/* ITrayEditPartFactory */
public EditPart createTrayEditPart(EditPart context, Object model) {
return createEditPart(context, model);
}
/* ILabeledElement */
public Image getSmallImage(Object object) {
return BPELUIPlugin.INSTANCE.getImage(IBPELUIConstants.ICON_MESSAGEEXCHANGE_16);
}
public Image getLargeImage(Object object) {
return BPELUIPlugin.INSTANCE.getImage(IBPELUIConstants.ICON_MESSAGEEXCHANGE_32);
}
public String getTypeLabel(Object object) {
return Messages.MessageExchangesAdapter_TypeLabel;
}
public String getLabel(Object object) {
return Messages.MessageExchangesAdapter_Label;
}
/* IOutlineEditPartFactory */
public EditPart createOutlineEditPart(EditPart context, final Object model) {
EditPart result = new OutlineTreeEditPart(){
protected List getModelChildren() {
MessageExchanges messageExchanges = (MessageExchanges) model;
List list = messageExchanges.getChildren();
return list;
}
};
result.setModel(model);
return result;
}
}
| chanakaudaya/developer-studio | bps/org.eclipse.bpel.ui/src/org/eclipse/bpel/ui/adapters/MessageExchangesAdapter.java | Java | apache-2.0 | 2,792 |
/**
* Licensed to Apereo under one or more contributor license
* agreements. See the NOTICE file distributed with this work
* for additional information regarding copyright ownership.
* Apereo licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a
* copy of the License at the following location:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jasig.portal.layout.dlm;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jasig.portal.PortalException;
import org.jasig.portal.layout.IUserLayoutStore;
import org.jasig.portal.security.IPerson;
import org.jasig.portal.spring.locator.UserLayoutStoreLocator;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
* Looks for, applies against the ilf, and updates accordingly the delete
* set within a plf.
*
* @version $Revision$ $Date$
* @since uPortal 2.5
*/
public class DeleteManager
{
private static final Log LOG = LogFactory.getLog(DeleteManager.class);
private static IUserLayoutStore dls = null;
/**
* Hands back the single instance of RDBMDistributedLayoutStore. There is
* already a method
* for aquiring a single instance of the configured layout store so we
* delegate over there so that all references refer to the same instance.
* This method is solely for convenience so that we don't have to keep
* calling UserLayoutStoreFactory and casting the resulting class.
*/
private static IUserLayoutStore getDLS()
{
if ( dls == null )
{
dls = UserLayoutStoreLocator.getUserLayoutStore();
}
return dls;
}
/**
Get the delete set if any from the plf and process each delete command
removing any that fail from the delete set so that the delete set is
self cleaning.
*/
static void applyAndUpdateDeleteSet( Document plf,
Document ilf,
IntegrationResult result )
{
Element dSet = null;
try
{
dSet = getDeleteSet( plf, null, false );
}
catch( Exception e )
{
LOG.error("Exception occurred while getting user's DLM delete-set.",
e);
}
if ( dSet == null )
return;
NodeList deletes = dSet.getChildNodes();
for( int i=deletes.getLength()-1; i>=0; i-- )
{
if ( applyDelete( (Element) deletes.item(i), ilf ) == false )
{
dSet.removeChild( deletes.item(i) );
result.setChangedPLF(true);
}
else
{
result.setChangedILF(true);
}
}
if ( dSet.getChildNodes().getLength() == 0 )
{
plf.getDocumentElement().removeChild( dSet );
result.setChangedPLF(true);
}
}
/**
Attempt to apply a single delete command and return true if it succeeds
or false otherwise. If the delete is disallowed or the target element
no longer exists in the document the delete command fails and returns
false.
*/
private static boolean applyDelete( Element delete, Document ilf )
{
String nodeID = delete.getAttribute( Constants.ATT_NAME );
Element e = ilf.getElementById( nodeID );
if ( e == null )
return false;
String deleteAllowed = e.getAttribute( Constants.ATT_DELETE_ALLOWED );
if ( deleteAllowed.equals( "false" ) )
return false;
Element p = (Element) e.getParentNode();
e.setIdAttribute(Constants.ATT_ID, false);
p.removeChild( e );
return true;
}
/**
Get the delete set if any stored in the root of the document or create
it is passed in create flag is true.
*/
private static Element getDeleteSet( Document plf,
IPerson person,
boolean create )
throws PortalException
{
Node root = plf.getDocumentElement();
Node child = root.getFirstChild();
while( child != null )
{
if ( child.getNodeName().equals( Constants.ELM_DELETE_SET ) )
return (Element) child;
child = child.getNextSibling();
}
if ( create == false )
return null;
String ID = null;
try
{
ID = getDLS().getNextStructDirectiveId( person );
}
catch (Exception e)
{
throw new PortalException( "Exception encountered while " +
"generating new delete set node " +
"Id for userId=" + person.getID(), e );
}
Element delSet = plf.createElement( Constants.ELM_DELETE_SET );
delSet.setAttribute( Constants.ATT_TYPE,
Constants.ELM_DELETE_SET );
delSet.setAttribute( Constants.ATT_ID, ID );
root.appendChild( delSet );
return delSet;
}
/**
Create and append a delete directive to delete the node identified by
the passed in element id. If this node contains any incorporated
elements then they must also have a delete directive added in here to
prevent incorporated channels originating in another column from
reappearing in that column because the position set entry that pulled
them into this column was now removed. (ie: the user moved an inc'd
channel to this column and then deleted the column means that the inc'd
channel should be deleted also.) This was designed to add a delete
directive for each nested element having an ID so as to work for the
future case of a tree view.
*/
public static void addDeleteDirective( Element compViewNode,
String elementID,
IPerson person )
throws PortalException
{
Document plf = (Document) person.getAttribute( Constants.PLF );
Element delSet = getDeleteSet( plf, person, true );
addDeleteDirective( compViewNode, elementID, person, plf, delSet );
}
/**
This method does the actual work of adding a delete directive and then
recursively calling itself for any incoporated children that need to be
deleted as well.
*/
private static void addDeleteDirective( Element compViewNode,
String elementID,
IPerson person,
Document plf,
Element delSet )
throws PortalException
{
String ID = null;
try
{
ID = getDLS().getNextStructDirectiveId( person );
}
catch (Exception e)
{
throw new PortalException( "Exception encountered while " +
"generating new delete node " +
"Id for userId=" + person.getID(), e );
}
Element delete = plf.createElement( Constants.ELM_DELETE );
delete.setAttribute( Constants.ATT_TYPE, Constants.ELM_DELETE );
delete.setAttribute( Constants.ATT_ID, ID );
delete.setAttributeNS( Constants.NS_URI,
Constants.ATT_NAME, elementID );
delSet.appendChild( delete );
// now pass through children and add delete directives for those with
// IDs indicating that they were incorporated
Element child = (Element) compViewNode.getFirstChild();
while( child != null )
{
String childID = child.getAttribute( "ID" );
if ( childID.startsWith( Constants.FRAGMENT_ID_USER_PREFIX ) )
addDeleteDirective( child, childID, person, plf, delSet );
child = (Element) child.getNextSibling();
}
}
}
| mgillian/uPortal | uportal-war/src/main/java/org/jasig/portal/layout/dlm/DeleteManager.java | Java | apache-2.0 | 8,567 |
/*
* Copyright (c) 2013-2016 Cinchapi Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.cinchapi.concourse.server.model;
import java.nio.ByteBuffer;
import java.util.Objects;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import com.cinchapi.concourse.server.io.Byteable;
import com.cinchapi.concourse.util.ByteBuffers;
import com.google.common.base.Preconditions;
/**
* A Position is an abstraction for the association between a
* relative location and a {@link PrimaryKey} that is used in a
* {@link SearchRecord} to specify the location of a term in a record.
*
* @author Jeff Nelson
*/
@Immutable
public final class Position implements Byteable, Comparable<Position> {
/**
* Return the Position encoded in {@code bytes} so long as those bytes
* adhere to the format specified by the {@link #getBytes()} method. This
* method assumes that all the bytes in the {@code bytes} belong to the
* Position. In general, it is necessary to get the appropriate Position
* slice from the parent ByteBuffer using
* {@link ByteBuffers#slice(ByteBuffer, int, int)}.
*
* @param bytes
* @return the Position
*/
public static Position fromByteBuffer(ByteBuffer bytes) {
PrimaryKey primaryKey = PrimaryKey.fromByteBuffer(ByteBuffers.get(
bytes, PrimaryKey.SIZE));
int index = bytes.getInt();
return new Position(primaryKey, index);
}
/**
* Return a Position that is backed by {@code primaryKey} and {@code index}.
*
* @param primaryKey
* @param index
* @return the Position
*/
public static Position wrap(PrimaryKey primaryKey, int index) {
return new Position(primaryKey, index);
}
/**
* The total number of bytes used to store each Position
*/
public static final int SIZE = PrimaryKey.SIZE + 4; // index
/**
* A cached copy of the binary representation that is returned from
* {@link #getBytes()}.
*/
private transient ByteBuffer bytes;
/**
* The index that this Position represents.
*/
private final int index;
/**
* The PrimaryKey of the record that this Position represents.
*/
private final PrimaryKey primaryKey;
/**
* Construct a new instance.
*
* @param primaryKey
* @param index
*/
private Position(PrimaryKey primaryKey, int index) {
this(primaryKey, index, null);
}
/**
* Construct a new instance.
*
* @param primaryKey
* @param index
* @param bytes;
*/
private Position(PrimaryKey primaryKey, int index,
@Nullable ByteBuffer bytes) {
Preconditions
.checkArgument(index >= 0, "Cannot have an negative index");
this.primaryKey = primaryKey;
this.index = index;
this.bytes = bytes;
}
@Override
public int compareTo(Position other) {
int comparison;
return (comparison = primaryKey.compareTo(other.primaryKey)) != 0 ? comparison
: Integer.compare(index, other.index);
}
@Override
public boolean equals(Object obj) {
if(obj instanceof Position) {
Position other = (Position) obj;
return primaryKey.equals(other.primaryKey) && index == other.index;
}
return false;
}
/**
* Return a byte buffer that represents this Value with the following order:
* <ol>
* <li><strong>primaryKey</strong> - position 0</li>
* <li><strong>index</strong> - position 8</li>
* </ol>
*
* @return the ByteBuffer representation
*/
@Override
public ByteBuffer getBytes() {
if(bytes == null) {
bytes = ByteBuffer.allocate(size());
copyTo(bytes);
bytes.rewind();
}
return ByteBuffers.asReadOnlyBuffer(bytes);
}
/**
* Return the associated {@code index}.
*
* @return the index
*/
public int getIndex() {
return index;
}
/**
* Return the associated {@code primaryKey}.
*
* @return the primaryKey
*/
public PrimaryKey getPrimaryKey() {
return primaryKey;
}
@Override
public int hashCode() {
return Objects.hash(primaryKey, index);
}
@Override
public int size() {
return SIZE;
}
@Override
public String toString() {
return "Position " + index + " in Record " + primaryKey;
}
@Override
public void copyTo(ByteBuffer buffer) {
// NOTE: Storing the index as an int instead of some size aware
// variable length is probably overkill since most indexes will be
// smaller than Byte.MAX_SIZE or Short.MAX_SIZE, but having variable
// size indexes means that the size of the entire Position (as an
// int) must be stored before the Position for proper
// deserialization. By storing the index as an int, the size of each
// Position is constant so we won't need to store the overall size
// prior to the Position to deserialize it, which is actually more
// space efficient.
primaryKey.copyTo(buffer);
buffer.putInt(index);
}
}
| remiemalik/concourse | concourse-server/src/main/java/com/cinchapi/concourse/server/model/Position.java | Java | apache-2.0 | 5,815 |
// Copyright 2012 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.api.ads.common.lib.soap;
/**
* Used to package a SOAP call's return. Contains the return value, the request
* and response info, and the originating {@link SoapCall}.
*
* @author Adam Rogal
*/
public class SoapCallReturn {
private Object returnValue;
private RequestInfo requestInfo;
private ResponseInfo responseInfo;
private Throwable exception;
/**
* Constructor.
*/
public SoapCallReturn(){
requestInfo = new RequestInfo();
responseInfo = new ResponseInfo();
}
/**
* Gets the return value from the SOAP call that was made.
*
* @return the return value from the SOAP call that was made or {@code null}
* if there was an exception
*/
public Object getReturnValue() {
return returnValue;
}
/**
* Gets the request info from the SOAP call that was made.
*/
public RequestInfo getRequestInfo() {
return requestInfo;
}
/**
* Gets the response info from the SOAP call that was made.
*/
public ResponseInfo getResponseInfo() {
return responseInfo;
}
/**
* Gets the exception from the SOAP call that was made if one occurred.
*
* @return the exception from the SOAP call that was made or {@code null}
* if there was no exception
*/
public Throwable getException() {
return exception;
}
/**
* Builder for {@link SoapCallReturn} objects.
*
* @author Adam Rogal
*/
public static class Builder {
private SoapCallReturn soapCallReturn;
/**
* Constructor.
*/
public Builder() {
this.soapCallReturn = new SoapCallReturn();
}
/**
* Adds a return value to the SoapCallReturn under construction.
*
* @param returnValue the return value to add to the SoapCallReturn
* @return this builder
*/
public Builder withReturnValue(Object returnValue) {
soapCallReturn.returnValue = returnValue;
return this;
}
/**
* Adds a response info to the SoapCallReturn under construction.
*
* @param responseInfo the response info to add to the SoapCallReturn
* @return this builder
*/
public Builder withResponseInfo(ResponseInfo responseInfo) {
soapCallReturn.responseInfo = responseInfo;
return this;
}
/**
* Adds a request info to the SoapCallReturn under construction.
*
* @param requestInfo the request info to add to the SoapCallReturn
* @return this builder
*/
public Builder withRequestInfo(RequestInfo requestInfo) {
soapCallReturn.requestInfo = requestInfo;
return this;
}
/**
* Adds an exception to the SoapCallReturn under construction.
*
* @param exception the exception to add to the SoapCallReturn
* @return this builder
*/
public Builder withException(Throwable exception) {
soapCallReturn.exception = exception;
return this;
}
/**
* Returns the SoapCallReturn this Builder has been constructing.
*
* @return the built SoapCallReturn object
*/
public SoapCallReturn build() {
return soapCallReturn;
}
}
}
| andyj24/googleads-java-lib | modules/ads_lib/src/main/java/com/google/api/ads/common/lib/soap/SoapCallReturn.java | Java | apache-2.0 | 3,736 |
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.vxquery.compiler.rewriter.rules;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang3.mutable.Mutable;
import org.apache.vxquery.functions.BuiltinFunctions;
import org.apache.vxquery.functions.BuiltinOperators;
import org.apache.hyracks.algebricks.common.exceptions.AlgebricksException;
import org.apache.hyracks.algebricks.common.utils.Pair;
import org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression;
import org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator;
import org.apache.hyracks.algebricks.core.algebra.base.IOptimizationContext;
import org.apache.hyracks.algebricks.core.algebra.base.LogicalExpressionTag;
import org.apache.hyracks.algebricks.core.algebra.base.LogicalOperatorTag;
import org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression;
import org.apache.hyracks.algebricks.core.algebra.expressions.AggregateFunctionCallExpression;
import org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier;
import org.apache.hyracks.algebricks.core.algebra.functions.IFunctionInfo;
import org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator;
import org.apache.hyracks.algebricks.core.algebra.operators.logical.AggregateOperator;
import org.apache.hyracks.algebricks.core.rewriter.base.IAlgebraicRewriteRule;
/**
* The rule searches for aggregate operators with an aggregate function
* expression that has not been initialized for two step aggregation.
*
* <pre>
* Before
*
* plan__parent
* AGGREGATE( $v : af1( $v1 ) )
* plan__child
*
* Where af1 is a VXquery aggregate function expression configured for single
* step processing and $v1 is defined in plan__child.
*
* After
*
* if (af1 == count) aggregate operating settings:
* Step 1: count
* Step 2: sum
* if (af1 == avg) aggregate operating settings:
* Step 1: avg-local
* Step 2: avg-global
* if (af1 in (max, min, sum)) aggregate operating settings:
* Step 1: af1
* Step 2: af1
* </pre>
*
* @author prestonc
*/
public class IntroduceTwoStepAggregateRule implements IAlgebraicRewriteRule {
final Map<FunctionIdentifier, Pair<IFunctionInfo, IFunctionInfo>> AGGREGATE_MAP = new HashMap<FunctionIdentifier, Pair<IFunctionInfo, IFunctionInfo>>();
public IntroduceTwoStepAggregateRule() {
AGGREGATE_MAP.put(BuiltinFunctions.FN_AVG_1.getFunctionIdentifier(),
new Pair<IFunctionInfo, IFunctionInfo>(BuiltinOperators.AVG_LOCAL, BuiltinOperators.AVG_GLOBAL));
AGGREGATE_MAP.put(BuiltinFunctions.FN_COUNT_1.getFunctionIdentifier(),
new Pair<IFunctionInfo, IFunctionInfo>(BuiltinFunctions.FN_COUNT_1, BuiltinFunctions.FN_SUM_1));
AGGREGATE_MAP.put(BuiltinFunctions.FN_MAX_1.getFunctionIdentifier(),
new Pair<IFunctionInfo, IFunctionInfo>(BuiltinFunctions.FN_MAX_1, BuiltinFunctions.FN_MAX_1));
AGGREGATE_MAP.put(BuiltinFunctions.FN_MIN_1.getFunctionIdentifier(),
new Pair<IFunctionInfo, IFunctionInfo>(BuiltinFunctions.FN_MIN_1, BuiltinFunctions.FN_MIN_1));
AGGREGATE_MAP.put(BuiltinFunctions.FN_SUM_1.getFunctionIdentifier(),
new Pair<IFunctionInfo, IFunctionInfo>(BuiltinFunctions.FN_SUM_1, BuiltinFunctions.FN_SUM_1));
}
@Override
public boolean rewritePre(Mutable<ILogicalOperator> opRef, IOptimizationContext context)
throws AlgebricksException {
// Check if aggregate function.
AbstractLogicalOperator op = (AbstractLogicalOperator) opRef.getValue();
if (op.getOperatorTag() != LogicalOperatorTag.AGGREGATE) {
return false;
}
AggregateOperator aggregate = (AggregateOperator) op;
if (aggregate.getExpressions().size() == 0) {
return false;
}
Mutable<ILogicalExpression> mutableLogicalExpression = aggregate.getExpressions().get(0);
ILogicalExpression logicalExpression = mutableLogicalExpression.getValue();
if (logicalExpression.getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) {
return false;
}
AbstractFunctionCallExpression functionCall = (AbstractFunctionCallExpression) logicalExpression;
if (AGGREGATE_MAP.containsKey(functionCall.getFunctionIdentifier())) {
AggregateFunctionCallExpression aggregateFunctionCall = (AggregateFunctionCallExpression) functionCall;
if (aggregateFunctionCall.isTwoStep()) {
return false;
}
aggregateFunctionCall.setTwoStep(true);
aggregateFunctionCall.setStepOneAggregate(AGGREGATE_MAP.get(functionCall.getFunctionIdentifier()).first);
aggregateFunctionCall.setStepTwoAggregate(AGGREGATE_MAP.get(functionCall.getFunctionIdentifier()).second);
return true;
}
return false;
}
@Override
public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context) {
return false;
}
}
| prestoncarman/vxquery | vxquery-core/src/main/java/org/apache/vxquery/compiler/rewriter/rules/IntroduceTwoStepAggregateRule.java | Java | apache-2.0 | 5,872 |
/*
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.openapi.roots.ui.configuration;
import com.intellij.ide.DataManager;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectBundle;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.projectRoots.SdkType;
import com.intellij.openapi.projectRoots.SdkTypeId;
import com.intellij.openapi.roots.ui.OrderEntryAppearanceService;
import com.intellij.openapi.roots.ui.configuration.projectRoot.JdkListConfigurable;
import com.intellij.openapi.roots.ui.configuration.projectRoot.ProjectSdksModel;
import com.intellij.openapi.ui.ComboBoxWithWidePopup;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.openapi.util.Computable;
import com.intellij.openapi.util.Condition;
import com.intellij.openapi.util.Conditions;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.ui.ColoredListCellRenderer;
import com.intellij.ui.ScreenUtil;
import com.intellij.ui.SimpleTextAttributes;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.ui.EmptyIcon;
import com.intellij.util.ui.JBUI;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.awt.*;
import java.util.Arrays;
import java.util.Collection;
/**
* @author Eugene Zhuravlev
* @since May 18, 2005
*/
public class JdkComboBox extends ComboBoxWithWidePopup<JdkComboBox.JdkComboBoxItem> {
private static final Icon EMPTY_ICON = JBUI.scale(EmptyIcon.create(1, 16));
@Nullable
private final Condition<Sdk> myFilter;
@Nullable
private final Condition<SdkTypeId> myCreationFilter;
private JButton mySetUpButton;
private final Condition<SdkTypeId> mySdkTypeFilter;
public JdkComboBox(@NotNull final ProjectSdksModel jdkModel) {
this(jdkModel, null);
}
public JdkComboBox(@NotNull final ProjectSdksModel jdkModel,
@Nullable Condition<SdkTypeId> filter) {
this(jdkModel, filter, getSdkFilter(filter), filter, false);
}
public JdkComboBox(@NotNull final ProjectSdksModel jdkModel,
@Nullable Condition<SdkTypeId> sdkTypeFilter,
@Nullable Condition<Sdk> filter,
@Nullable Condition<SdkTypeId> creationFilter,
boolean addSuggestedItems) {
super(new JdkComboBoxModel(jdkModel, sdkTypeFilter, filter, addSuggestedItems));
myFilter = filter;
mySdkTypeFilter = sdkTypeFilter;
myCreationFilter = creationFilter;
setRenderer(new ColoredListCellRenderer<JdkComboBoxItem>() {
@Override
protected void customizeCellRenderer(@NotNull JList<? extends JdkComboBoxItem> list,
JdkComboBoxItem value,
int index,
boolean selected,
boolean hasFocus) {
if (JdkComboBox.this.isEnabled()) {
setIcon(EMPTY_ICON); // to fix vertical size
if (value instanceof InvalidJdkComboBoxItem) {
final String str = value.toString();
append(str, SimpleTextAttributes.ERROR_ATTRIBUTES);
}
else if (value instanceof ProjectJdkComboBoxItem) {
final Sdk jdk = jdkModel.getProjectSdk();
if (jdk != null) {
setIcon(((SdkType)jdk.getSdkType()).getIcon());
append(ProjectBundle.message("project.roots.project.jdk.inherited"), SimpleTextAttributes.REGULAR_ATTRIBUTES);
append(" (" + jdk.getName() + ")", SimpleTextAttributes.GRAYED_ATTRIBUTES);
}
else {
final String str = value.toString();
append(str, SimpleTextAttributes.ERROR_ATTRIBUTES);
}
}
else if (value instanceof SuggestedJdkItem) {
SdkType type = ((SuggestedJdkItem)value).getSdkType();
String home = ((SuggestedJdkItem)value).getPath();
setIcon(type.getIconForAddAction());
String version = type.getVersionString(home);
append(version == null ? type.getPresentableName() : version);
append(" (" + home + ")", SimpleTextAttributes.GRAYED_ATTRIBUTES);
}
else if (value != null) {
OrderEntryAppearanceService.getInstance().forJdk(value.getJdk(), false, selected, true).customize(this);
}
else {
customizeCellRenderer(list, new NoneJdkComboBoxItem(), index, selected, hasFocus);
}
}
}
});
}
@Override
public Dimension getPreferredSize() {
final Rectangle rec = ScreenUtil.getScreenRectangle(0, 0);
final Dimension size = super.getPreferredSize();
final int maxWidth = rec.width / 4;
if (size.width > maxWidth) {
size.width = maxWidth;
}
return size;
}
@Override
public Dimension getMinimumSize() {
final Dimension minSize = super.getMinimumSize();
final Dimension prefSize = getPreferredSize();
if (minSize.width > prefSize.width) {
minSize.width = prefSize.width;
}
return minSize;
}
public void setSetupButton(final JButton setUpButton,
@Nullable final Project project,
final ProjectSdksModel jdksModel,
final JdkComboBoxItem firstItem,
@Nullable final Condition<Sdk> additionalSetup,
final boolean moduleJdkSetup) {
setSetupButton(setUpButton, project, jdksModel, firstItem, additionalSetup,
ProjectBundle.message("project.roots.set.up.jdk.title", moduleJdkSetup ? 1 : 2));
}
public void setSetupButton(final JButton setUpButton,
@Nullable final Project project,
final ProjectSdksModel jdksModel,
final JdkComboBoxItem firstItem,
@Nullable final Condition<Sdk> additionalSetup,
final String actionGroupTitle) {
mySetUpButton = setUpButton;
mySetUpButton.addActionListener(e -> {
DefaultActionGroup group = new DefaultActionGroup();
jdksModel.createAddActions(group, this, getSelectedJdk(), jdk -> {
if (project != null) {
final JdkListConfigurable configurable = JdkListConfigurable.getInstance(project);
configurable.addJdkNode(jdk, false);
}
reloadModel(new ActualJdkComboBoxItem(jdk), project);
setSelectedJdk(jdk); //restore selection
if (additionalSetup != null) {
if (additionalSetup.value(jdk)) { //leave old selection
setSelectedJdk(firstItem.getJdk());
}
}
}, myCreationFilter);
final DataContext dataContext = DataManager.getInstance().getDataContext(this);
if (group.getChildrenCount() > 1) {
JBPopupFactory.getInstance()
.createActionGroupPopup(actionGroupTitle, group, dataContext, JBPopupFactory.ActionSelectionAid.MNEMONICS, false)
.showUnderneathOf(setUpButton);
}
else {
final AnActionEvent event =
new AnActionEvent(null, dataContext, ActionPlaces.UNKNOWN, new Presentation(""), ActionManager.getInstance(), 0);
group.getChildren(event)[0].actionPerformed(event);
}
});
}
public void setEditButton(final JButton editButton, final Project project, final Computable<Sdk> retrieveJDK){
editButton.addActionListener(e -> {
final Sdk projectJdk = retrieveJDK.compute();
if (projectJdk != null) {
ProjectStructureConfigurable.getInstance(project).select(projectJdk, true);
}
});
addActionListener(e -> {
final JdkComboBoxItem selectedItem = getSelectedItem();
if (selectedItem instanceof ProjectJdkComboBoxItem) {
editButton.setEnabled(ProjectStructureConfigurable.getInstance(project).getProjectJdksModel().getProjectSdk() != null);
}
else {
editButton.setEnabled(!(selectedItem instanceof InvalidJdkComboBoxItem) && selectedItem != null && selectedItem.getJdk() != null);
}
});
}
public JButton getSetUpButton() {
return mySetUpButton;
}
@Override
public JdkComboBoxItem getSelectedItem() {
return (JdkComboBoxItem)super.getSelectedItem();
}
@Nullable
public Sdk getSelectedJdk() {
final JdkComboBoxItem selectedItem = getSelectedItem();
return selectedItem != null? selectedItem.getJdk() : null;
}
public void setSelectedJdk(Sdk jdk) {
final int index = indexOf(jdk);
if (index >= 0) {
setSelectedIndex(index);
}
}
public void setInvalidJdk(String name) {
removeInvalidElement();
addItem(new InvalidJdkComboBoxItem(name));
setSelectedIndex(getModel().getSize() - 1);
}
private int indexOf(Sdk jdk) {
final JdkComboBoxModel model = (JdkComboBoxModel)getModel();
final int count = model.getSize();
for (int idx = 0; idx < count; idx++) {
final JdkComboBoxItem elementAt = model.getElementAt(idx);
if (jdk == null) {
if (elementAt instanceof NoneJdkComboBoxItem || elementAt instanceof ProjectJdkComboBoxItem) {
return idx;
}
}
else {
Sdk elementAtJdk = elementAt.getJdk();
if (elementAtJdk != null && jdk.getName().equals(elementAtJdk.getName())) {
return idx;
}
}
}
return -1;
}
private void removeInvalidElement() {
final JdkComboBoxModel model = (JdkComboBoxModel)getModel();
final int count = model.getSize();
for (int idx = 0; idx < count; idx++) {
final JdkComboBoxItem elementAt = model.getElementAt(idx);
if (elementAt instanceof InvalidJdkComboBoxItem) {
removeItemAt(idx);
break;
}
}
}
public void reloadModel(JdkComboBoxItem firstItem, @Nullable Project project) {
final JdkComboBoxModel model = (JdkComboBoxModel)getModel();
if (project == null) {
model.addElement(firstItem);
return;
}
model.reload(firstItem, ProjectStructureConfigurable.getInstance(project).getProjectJdksModel(), mySdkTypeFilter, myFilter, false);
}
private static class JdkComboBoxModel extends DefaultComboBoxModel<JdkComboBoxItem> {
JdkComboBoxModel(@NotNull final ProjectSdksModel jdksModel, @Nullable Condition<SdkTypeId> sdkTypeFilter,
@Nullable Condition<Sdk> sdkFilter, boolean addSuggested) {
reload(null, jdksModel, sdkTypeFilter, sdkFilter, addSuggested);
}
void reload(@Nullable final JdkComboBoxItem firstItem,
@NotNull final ProjectSdksModel jdksModel,
@Nullable Condition<SdkTypeId> sdkTypeFilter,
@Nullable Condition<Sdk> sdkFilter,
boolean addSuggested) {
removeAllElements();
if (firstItem != null) addElement(firstItem);
Sdk[] jdks = sortSdks(jdksModel.getSdks());
for (Sdk jdk : jdks) {
if (sdkFilter == null || sdkFilter.value(jdk)) {
addElement(new ActualJdkComboBoxItem(jdk));
}
}
if (addSuggested) {
addSuggestedItems(sdkTypeFilter, jdks);
}
}
@NotNull
private static Sdk[] sortSdks(@NotNull final Sdk[] sdks) {
Sdk[] clone = sdks.clone();
Arrays.sort(clone, (sdk1, sdk2) -> {
SdkType sdkType1 = (SdkType)sdk1.getSdkType();
SdkType sdkType2 = (SdkType)sdk2.getSdkType();
if (!sdkType1.getComparator().equals(sdkType2.getComparator())) return StringUtil.compare(sdkType1.getPresentableName(), sdkType2.getPresentableName(), true);
return sdkType1.getComparator().compare(sdk1, sdk2);
});
return clone;
}
void addSuggestedItems(@Nullable Condition<SdkTypeId> sdkTypeFilter, Sdk[] jdks) {
SdkType[] types = SdkType.getAllTypes();
for (SdkType type : types) {
if (sdkTypeFilter == null || sdkTypeFilter.value(type) && ContainerUtil.find(jdks, sdk -> sdk.getSdkType() == type) == null) {
Collection<String> paths = type.suggestHomePaths();
for (String path : paths) {
if (path != null && type.isValidSdkHome(path)) {
addElement(new SuggestedJdkItem(type, path));
}
}
}
}
}
}
public static Condition<Sdk> getSdkFilter(@Nullable final Condition<SdkTypeId> filter) {
return filter == null ? Conditions.alwaysTrue() : sdk -> filter.value(sdk.getSdkType());
}
public abstract static class JdkComboBoxItem {
@Nullable
public Sdk getJdk() {
return null;
}
@Nullable
public String getSdkName() {
return null;
}
}
public static class ActualJdkComboBoxItem extends JdkComboBoxItem {
private final Sdk myJdk;
public ActualJdkComboBoxItem(@NotNull Sdk jdk) {
myJdk = jdk;
}
@Override
public String toString() {
return myJdk.getName();
}
@Nullable
@Override
public Sdk getJdk() {
return myJdk;
}
@Nullable
@Override
public String getSdkName() {
return myJdk.getName();
}
}
public static class ProjectJdkComboBoxItem extends JdkComboBoxItem {
public String toString() {
return ProjectBundle.message("jdk.combo.box.project.item");
}
}
public static class NoneJdkComboBoxItem extends JdkComboBoxItem {
public String toString() {
return ProjectBundle.message("jdk.combo.box.none.item");
}
}
private static class InvalidJdkComboBoxItem extends JdkComboBoxItem {
private final String mySdkName;
InvalidJdkComboBoxItem(String name) {
mySdkName = name;
}
@Override
public String getSdkName() {
return mySdkName;
}
public String toString() {
return ProjectBundle.message("jdk.combo.box.invalid.item", mySdkName);
}
}
public static class SuggestedJdkItem extends JdkComboBoxItem {
private final SdkType mySdkType;
private final String myPath;
SuggestedJdkItem(@NotNull SdkType sdkType, @NotNull String path) {
mySdkType = sdkType;
myPath = path;
}
@NotNull
public SdkType getSdkType() {
return mySdkType;
}
@NotNull
public String getPath() {
return myPath;
}
@Override
public String toString() {
return myPath;
}
}
}
| ThiagoGarciaAlves/intellij-community | java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/JdkComboBox.java | Java | apache-2.0 | 15,058 |
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.hadoop.yarn.server.resourcemanager.scheduler.fair;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.classification.InterfaceAudience.Private;
import org.apache.hadoop.classification.InterfaceStability.Unstable;
import org.apache.hadoop.security.UserGroupInformation;
import org.apache.hadoop.yarn.api.records.ApplicationAttemptId;
import org.apache.hadoop.yarn.api.records.QueueACL;
import org.apache.hadoop.yarn.api.records.QueueUserACLInfo;
import org.apache.hadoop.yarn.api.records.Resource;
import org.apache.hadoop.yarn.server.resourcemanager.rmcontainer.RMContainer;
import org.apache.hadoop.yarn.server.resourcemanager.scheduler.ActiveUsersManager;
import org.apache.hadoop.yarn.server.resourcemanager.scheduler.SchedulerAppUtils;
import org.apache.hadoop.yarn.server.resourcemanager.scheduler.SchedulerApplicationAttempt;
import org.apache.hadoop.yarn.util.resource.Resources;
@Private
@Unstable
public class FSLeafQueue extends FSQueue {
private static final Log LOG = LogFactory.getLog(
FSLeafQueue.class.getName());
private final List<AppSchedulable> runnableAppScheds = // apps that are runnable
new ArrayList<AppSchedulable>();
private final List<AppSchedulable> nonRunnableAppScheds =
new ArrayList<AppSchedulable>();
private Resource demand = Resources.createResource(0);
// Variables used for preemption
private long lastTimeAtMinShare;
private long lastTimeAtHalfFairShare;
// Track the AM resource usage for this queue
private Resource amResourceUsage;
private final ActiveUsersManager activeUsersManager;
public FSLeafQueue(String name, FairScheduler scheduler,
FSParentQueue parent) {
super(name, scheduler, parent);
this.lastTimeAtMinShare = scheduler.getClock().getTime();
this.lastTimeAtHalfFairShare = scheduler.getClock().getTime();
activeUsersManager = new ActiveUsersManager(getMetrics());
amResourceUsage = Resource.newInstance(0, 0);
}
public void addApp(FSSchedulerApp app, boolean runnable) {
AppSchedulable appSchedulable = new AppSchedulable(scheduler, app, this);
app.setAppSchedulable(appSchedulable);
if (runnable) {
runnableAppScheds.add(appSchedulable);
} else {
nonRunnableAppScheds.add(appSchedulable);
}
}
// for testing
void addAppSchedulable(AppSchedulable appSched) {
runnableAppScheds.add(appSched);
}
/**
* Removes the given app from this queue.
* @return whether or not the app was runnable
*/
public boolean removeApp(FSSchedulerApp app) {
if (runnableAppScheds.remove(app.getAppSchedulable())) {
// Update AM resource usage
if (app.isAmRunning() && app.getAMResource() != null) {
Resources.subtractFrom(amResourceUsage, app.getAMResource());
}
return true;
} else if (nonRunnableAppScheds.remove(app.getAppSchedulable())) {
return false;
} else {
throw new IllegalStateException("Given app to remove " + app +
" does not exist in queue " + this);
}
}
public Collection<AppSchedulable> getRunnableAppSchedulables() {
return runnableAppScheds;
}
public List<AppSchedulable> getNonRunnableAppSchedulables() {
return nonRunnableAppScheds;
}
@Override
public void collectSchedulerApplications(
Collection<ApplicationAttemptId> apps) {
for (AppSchedulable appSched : runnableAppScheds) {
apps.add(appSched.getApp().getApplicationAttemptId());
}
for (AppSchedulable appSched : nonRunnableAppScheds) {
apps.add(appSched.getApp().getApplicationAttemptId());
}
}
@Override
public void setPolicy(SchedulingPolicy policy)
throws AllocationConfigurationException {
if (!SchedulingPolicy.isApplicableTo(policy, SchedulingPolicy.DEPTH_LEAF)) {
throwPolicyDoesnotApplyException(policy);
}
super.policy = policy;
}
@Override
public void recomputeShares() {
policy.computeShares(getRunnableAppSchedulables(), getFairShare());
}
@Override
public Resource getDemand() {
return demand;
}
@Override
public Resource getResourceUsage() {
Resource usage = Resources.createResource(0);
for (AppSchedulable app : runnableAppScheds) {
Resources.addTo(usage, app.getResourceUsage());
}
for (AppSchedulable app : nonRunnableAppScheds) {
Resources.addTo(usage, app.getResourceUsage());
}
return usage;
}
public Resource getAmResourceUsage() {
return amResourceUsage;
}
@Override
public void updateDemand() {
// Compute demand by iterating through apps in the queue
// Limit demand to maxResources
Resource maxRes = scheduler.getAllocationConfiguration()
.getMaxResources(getName());
demand = Resources.createResource(0);
for (AppSchedulable sched : runnableAppScheds) {
if (Resources.equals(demand, maxRes)) {
break;
}
updateDemandForApp(sched, maxRes);
}
for (AppSchedulable sched : nonRunnableAppScheds) {
if (Resources.equals(demand, maxRes)) {
break;
}
updateDemandForApp(sched, maxRes);
}
if (LOG.isDebugEnabled()) {
LOG.debug("The updated demand for " + getName() + " is " + demand
+ "; the max is " + maxRes);
}
}
private void updateDemandForApp(AppSchedulable sched, Resource maxRes) {
sched.updateDemand();
Resource toAdd = sched.getDemand();
if (LOG.isDebugEnabled()) {
LOG.debug("Counting resource from " + sched.getName() + " " + toAdd
+ "; Total resource consumption for " + getName() + " now "
+ demand);
}
demand = Resources.add(demand, toAdd);
demand = Resources.componentwiseMin(demand, maxRes);
}
@Override
public Resource assignContainer(FSSchedulerNode node) {
Resource assigned = Resources.none();
if (LOG.isDebugEnabled()) {
LOG.debug("Node " + node.getNodeName() + " offered to queue: " + getName());
}
if (!assignContainerPreCheck(node)) {
return assigned;
}
Comparator<Schedulable> comparator = policy.getComparator();
Collections.sort(runnableAppScheds, comparator);
for (AppSchedulable sched : runnableAppScheds) {
if (SchedulerAppUtils.isBlacklisted(sched.getApp(), node, LOG)) {
continue;
}
assigned = sched.assignContainer(node);
if (!assigned.equals(Resources.none())) {
break;
}
}
return assigned;
}
@Override
public RMContainer preemptContainer() {
RMContainer toBePreempted = null;
if (LOG.isDebugEnabled()) {
LOG.debug("Queue " + getName() + " is going to preempt a container " +
"from its applications.");
}
// If this queue is not over its fair share, reject
if (!preemptContainerPreCheck()) {
return toBePreempted;
}
// Choose the app that is most over fair share
Comparator<Schedulable> comparator = policy.getComparator();
AppSchedulable candidateSched = null;
for (AppSchedulable sched : runnableAppScheds) {
if (candidateSched == null ||
comparator.compare(sched, candidateSched) > 0) {
candidateSched = sched;
}
}
// Preempt from the selected app
if (candidateSched != null) {
toBePreempted = candidateSched.preemptContainer();
}
return toBePreempted;
}
@Override
public List<FSQueue> getChildQueues() {
return new ArrayList<FSQueue>(1);
}
@Override
public List<QueueUserACLInfo> getQueueUserAclInfo(UserGroupInformation user) {
QueueUserACLInfo userAclInfo =
recordFactory.newRecordInstance(QueueUserACLInfo.class);
List<QueueACL> operations = new ArrayList<QueueACL>();
for (QueueACL operation : QueueACL.values()) {
if (hasAccess(operation, user)) {
operations.add(operation);
}
}
userAclInfo.setQueueName(getQueueName());
userAclInfo.setUserAcls(operations);
return Collections.singletonList(userAclInfo);
}
public long getLastTimeAtMinShare() {
return lastTimeAtMinShare;
}
public void setLastTimeAtMinShare(long lastTimeAtMinShare) {
this.lastTimeAtMinShare = lastTimeAtMinShare;
}
public long getLastTimeAtHalfFairShare() {
return lastTimeAtHalfFairShare;
}
public void setLastTimeAtHalfFairShare(long lastTimeAtHalfFairShare) {
this.lastTimeAtHalfFairShare = lastTimeAtHalfFairShare;
}
@Override
public int getNumRunnableApps() {
return runnableAppScheds.size();
}
@Override
public ActiveUsersManager getActiveUsersManager() {
return activeUsersManager;
}
/**
* Check whether this queue can run this application master under the
* maxAMShare limit
*
* @param amResource
* @return true if this queue can run
*/
public boolean canRunAppAM(Resource amResource) {
float maxAMShare =
scheduler.getAllocationConfiguration().getQueueMaxAMShare(getName());
if (Math.abs(maxAMShare - -1.0f) < 0.0001) {
return true;
}
Resource maxAMResource = Resources.multiply(getFairShare(), maxAMShare);
Resource ifRunAMResource = Resources.add(amResourceUsage, amResource);
return !policy
.checkIfAMResourceUsageOverLimit(ifRunAMResource, maxAMResource);
}
public void addAMResourceUsage(Resource amResource) {
if (amResource != null) {
Resources.addTo(amResourceUsage, amResource);
}
}
@Override
public void recoverContainer(Resource clusterResource,
SchedulerApplicationAttempt schedulerAttempt, RMContainer rmContainer) {
// TODO Auto-generated method stub
}
}
| tseen/Federated-HDFS | tseenliu/FedHDFS-hadoop-src/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/fair/FSLeafQueue.java | Java | apache-2.0 | 10,600 |
/*
* Copyright 2013 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.netflix.governator.lifecycle.warmup;
import com.google.inject.Singleton;
import com.netflix.governator.annotations.WarmUp;
public class Flat
{
/*
Root classes without dependencies
*/
@Singleton
public static class A
{
public volatile Recorder recorder;
@WarmUp
public void warmUp() throws InterruptedException
{
recorder.record("A");
}
}
@Singleton
public static class B
{
public volatile Recorder recorder;
@WarmUp
public void warmUp() throws InterruptedException
{
recorder.record("B");
}
}
}
| skinzer/governator | governator-legacy/src/test/java/com/netflix/governator/lifecycle/warmup/Flat.java | Java | apache-2.0 | 1,291 |
/* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.flowable.idm.engine.impl.cmd;
import java.io.Serializable;
import org.flowable.engine.common.api.FlowableIllegalArgumentException;
import org.flowable.engine.common.api.FlowableObjectNotFoundException;
import org.flowable.engine.common.impl.interceptor.Command;
import org.flowable.engine.common.impl.interceptor.CommandContext;
import org.flowable.idm.api.Picture;
import org.flowable.idm.api.User;
import org.flowable.idm.engine.impl.util.CommandContextUtil;
/**
* @author Tom Baeyens
*/
public class SetUserPictureCmd implements Command<Object>, Serializable {
private static final long serialVersionUID = 1L;
protected String userId;
protected Picture picture;
public SetUserPictureCmd(String userId, Picture picture) {
this.userId = userId;
this.picture = picture;
}
@Override
public Object execute(CommandContext commandContext) {
if (userId == null) {
throw new FlowableIllegalArgumentException("userId is null");
}
User user = CommandContextUtil.getIdmEngineConfiguration().getIdmIdentityService()
.createUserQuery().userId(userId)
.singleResult();
if (user == null) {
throw new FlowableObjectNotFoundException("user " + userId + " doesn't exist", User.class);
}
CommandContextUtil.getUserEntityManager(commandContext).setUserPicture(user, picture);
return null;
}
}
| zwets/flowable-engine | modules/flowable-idm-engine/src/main/java/org/flowable/idm/engine/impl/cmd/SetUserPictureCmd.java | Java | apache-2.0 | 2,022 |
/*
Copyright 2016 Goldman Sachs.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
package com.gs.fw.common.mithra.test.tax;
import com.gs.fw.finder.Operation;
import java.util.*;
public class FormRoleList extends FormRoleListAbstract
{
public FormRoleList()
{
super();
}
public FormRoleList(int initialSize)
{
super(initialSize);
}
public FormRoleList(Collection c)
{
super(c);
}
public FormRoleList(Operation operation)
{
super(operation);
}
}
| goldmansachs/reladomo | reladomo/src/test/java/com/gs/fw/common/mithra/test/tax/FormRoleList.java | Java | apache-2.0 | 1,001 |
/**
* Purpose of package - find largest number from 2 and 3 numbers.
* @since 1.0
* @author skuznetsov
* @version 2.0
*/
package ru.skuznetsov;
| kuznetsovsergeyymailcom/homework | chapter_001/maximumFromTwoNumbers/src/test/java/ru/skuznetsov/package-info.java | Java | apache-2.0 | 145 |
/*
* Copyright 2014 Guidewire Software, Inc.
*/
package gw.internal.gosu.parser;
import gw.lang.reflect.IMethodInfo;
/**
*/
public class ReducedDelegateFunctionSymbol extends ReducedDynamicFunctionSymbol implements IReducedDelegateFunctionSymbol {
private IMethodInfo _targetMethodInfo;
ReducedDelegateFunctionSymbol(DelegateFunctionSymbol dfs) {
super( dfs );
_targetMethodInfo = dfs.getMi();
}
@Override
public IMethodInfo getTargetMethodInfo() {
return _targetMethodInfo;
}
}
| dumitru-petrusca/gosu-lang | gosu-core/src/main/java/gw/internal/gosu/parser/ReducedDelegateFunctionSymbol.java | Java | apache-2.0 | 511 |
package org.apereo.cas.ticket.registry;
import org.apereo.cas.ticket.Ticket;
import org.infinispan.Cache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Collection;
import java.util.concurrent.TimeUnit;
/**
* This is {@link InfinispanTicketRegistry}. Infinispan is a distributed in-memory
* key/value data store with optional schema.
* It offers advanced functionality such as transactions, events, querying and distributed processing.
* See <a href="http://infinispan.org/features/">http://infinispan.org/features/</a> for more info.
*
* @author Misagh Moayyed
* @since 4.2.0
*/
public class InfinispanTicketRegistry extends AbstractTicketRegistry {
private static final Logger LOGGER = LoggerFactory.getLogger(InfinispanTicketRegistry.class);
private Cache<String, Ticket> cache;
/**
* Instantiates a new Infinispan ticket registry.
*
* @param cache the cache
*/
public InfinispanTicketRegistry(final Cache<String, Ticket> cache) {
this.cache = cache;
LOGGER.info("Setting up Infinispan Ticket Registry...");
}
@Override
public Ticket updateTicket(final Ticket ticket) {
this.cache.put(ticket.getId(), ticket);
return ticket;
}
@Override
public void addTicket(final Ticket ticketToAdd) {
final Ticket ticket = encodeTicket(ticketToAdd);
final long idleTime = ticket.getExpirationPolicy().getTimeToIdle() <= 0
? ticket.getExpirationPolicy().getTimeToLive()
: ticket.getExpirationPolicy().getTimeToIdle();
LOGGER.debug("Adding ticket [{}] to cache store to live [{}] seconds and stay idle for [{}]",
ticket.getId(), ticket.getExpirationPolicy().getTimeToLive(), idleTime);
this.cache.put(ticket.getId(), ticket,
ticket.getExpirationPolicy().getTimeToLive(), TimeUnit.SECONDS,
idleTime, TimeUnit.SECONDS);
}
@Override
public Ticket getTicket(final String ticketId) {
final String encTicketId = encodeTicketId(ticketId);
if (ticketId == null) {
return null;
}
return Ticket.class.cast(cache.get(encTicketId));
}
@Override
public boolean deleteSingleTicket(final String ticketId) {
this.cache.remove(ticketId);
return getTicket(ticketId) == null;
}
@Override
public long deleteAll() {
final int size = this.cache.size();
this.cache.clear();
return size;
}
/**
* Retrieve all tickets from the registry.
* <p>
* Note! Usage of this method can be computational and I/O intensive and should not be used for other than
* debugging.
*
* @return collection of tickets currently stored in the registry. Tickets
* might or might not be valid i.e. expired.
*/
@Override
public Collection<Ticket> getTickets() {
return decodeTickets(this.cache.values());
}
}
| petracvv/cas | support/cas-server-support-infinispan-ticket-registry/src/main/java/org/apereo/cas/ticket/registry/InfinispanTicketRegistry.java | Java | apache-2.0 | 2,994 |
/*
* Copyright 2010-2012 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package com.amazonaws.services.ec2.model.transform;
import java.util.Map;
import java.util.Map.Entry;
import javax.xml.stream.events.XMLEvent;
import com.amazonaws.services.ec2.model.*;
import com.amazonaws.transform.Unmarshaller;
import com.amazonaws.transform.MapEntry;
import com.amazonaws.transform.StaxUnmarshallerContext;
import com.amazonaws.transform.SimpleTypeStaxUnmarshallers.*;
/**
* Describe Tags Result StAX Unmarshaller
*/
public class DescribeTagsResultStaxUnmarshaller implements Unmarshaller<DescribeTagsResult, StaxUnmarshallerContext> {
public DescribeTagsResult unmarshall(StaxUnmarshallerContext context) throws Exception {
DescribeTagsResult describeTagsResult = new DescribeTagsResult();
int originalDepth = context.getCurrentDepth();
int targetDepth = originalDepth + 1;
if (context.isStartOfDocument()) targetDepth += 1;
while (true) {
XMLEvent xmlEvent = context.nextEvent();
if (xmlEvent.isEndDocument()) return describeTagsResult;
if (xmlEvent.isAttribute() || xmlEvent.isStartElement()) {
if (context.testExpression("tagSet/item", targetDepth)) {
describeTagsResult.getTags().add(TagDescriptionStaxUnmarshaller.getInstance().unmarshall(context));
continue;
}
} else if (xmlEvent.isEndElement()) {
if (context.getCurrentDepth() < originalDepth) {
return describeTagsResult;
}
}
}
}
private static DescribeTagsResultStaxUnmarshaller instance;
public static DescribeTagsResultStaxUnmarshaller getInstance() {
if (instance == null) instance = new DescribeTagsResultStaxUnmarshaller();
return instance;
}
}
| XidongHuang/aws-sdk-for-java | src/main/java/com/amazonaws/services/ec2/model/transform/DescribeTagsResultStaxUnmarshaller.java | Java | apache-2.0 | 2,423 |
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.isis.core.metamodel.facets.properties.property.disabled;
import org.apache.isis.applib.annotation.Editing;
import org.apache.isis.applib.annotation.Property;
import org.apache.isis.applib.annotation.When;
import org.apache.isis.applib.annotation.Where;
import org.apache.isis.core.metamodel.facetapi.FacetHolder;
import org.apache.isis.core.metamodel.facets.members.disabled.DisabledFacet;
import org.apache.isis.core.metamodel.facets.members.disabled.DisabledFacetAbstractImpl;
public class DisabledFacetForPropertyAnnotation extends DisabledFacetAbstractImpl {
public static DisabledFacet create(final Property property, final FacetHolder holder) {
if (property == null) {
return null;
}
final Editing editing = property.editing();
final String disabledReason = property.editingDisabledReason();
switch (editing) {
case AS_CONFIGURED:
// nothing needs to be done here; the DomainObjectFactory (processing @DomainObject annotation)
// will install an ImmutableFacetForDomainObjectAnnotation on the domain object and then a
// DisabledFacetOnPropertyDerivedFromImmutable facet will be installed.
return null;
case DISABLED:
return new DisabledFacetForPropertyAnnotation(disabledReason, holder);
case ENABLED:
return null;
}
return null;
}
private DisabledFacetForPropertyAnnotation(final String reason, final FacetHolder holder) {
super(When.ALWAYS, Where.EVERYWHERE, reason, holder);
}
}
| howepeng/isis | core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/properties/property/disabled/DisabledFacetForPropertyAnnotation.java | Java | apache-2.0 | 2,470 |
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.facebook.buck.infer;
import com.facebook.buck.core.config.BuckConfig;
import com.facebook.buck.core.config.ConfigView;
import com.facebook.buck.core.model.TargetConfiguration;
import com.facebook.buck.core.model.UnconfiguredBuildTarget;
import com.facebook.buck.core.sourcepath.SourcePath;
import com.facebook.buck.core.toolchain.toolprovider.ToolProvider;
import com.facebook.buck.core.toolchain.toolprovider.impl.ConstantToolProvider;
import com.facebook.buck.core.util.immutables.BuckStyleValue;
import com.facebook.buck.rules.tool.config.ToolConfig;
import com.google.common.collect.ImmutableList;
import java.nio.file.Paths;
import java.util.Optional;
import org.immutables.value.Value;
/** Infer specific buck config */
@BuckStyleValue
public abstract class InferConfig implements ConfigView<BuckConfig> {
// TODO(arr): change to just "infer" when cxx and java configs are consolidated
private static final String SECTION = "infer_java";
private static final String DIST_FIELD = "dist";
private static final String DEFAULT_DIST_BINARY = "infer";
@Override
public abstract BuckConfig getDelegate();
public static InferConfig of(BuckConfig delegate) {
return ImmutableInferConfig.of(delegate);
}
@Value.Lazy
public Optional<ToolProvider> getBinary() {
return getDelegate().getView(ToolConfig.class).getToolProvider(SECTION, "binary");
}
/**
* Depending on the type of dist (plain path vs target) either return a {@link
* ConstantToolProvider} or {@link InferDistFromTargetProvider} with properly set up parse time
* deps.
*/
@Value.Lazy
public Optional<ToolProvider> getDist() {
Optional<String> valueOpt = getDelegate().getValue(SECTION, DIST_FIELD);
if (!valueOpt.isPresent()) {
return Optional.empty();
}
String value = valueOpt.get();
Optional<UnconfiguredBuildTarget> targetOpt =
getDelegate().getMaybeUnconfiguredBuildTarget(SECTION, DIST_FIELD);
ToolProvider toolProvider =
targetOpt
.map(this::mkDistProviderFromTarget)
.orElseGet(() -> this.mkDistProviderFromPath(value));
return Optional.of(toolProvider);
}
@Value.Lazy
public String getDistBinary() {
return getDelegate().getValue(SECTION, "dist_binary").orElse(DEFAULT_DIST_BINARY);
}
@Value.Lazy
public Optional<String> getVersion() {
return getDelegate().getValue(SECTION, "version");
}
@Value.Lazy
public Optional<SourcePath> getConfigFile(TargetConfiguration targetConfiguration) {
return getDelegate().getSourcePath(SECTION, "config_file", targetConfiguration);
}
@Value.Lazy
public ImmutableList<String> getNullsafeArgs() {
return getDelegate().getListWithoutComments(SECTION, "nullsafe_args");
}
/** Directory with third party signatures for nullsafe. */
@Value.Lazy
public Optional<SourcePath> getNullsafeThirdPartySignatures(
TargetConfiguration targetConfiguration) {
return getDelegate()
.getSourcePath(SECTION, "nullsafe_third_party_signatures", targetConfiguration);
}
@Value.Lazy
public Boolean getPrettyPrint() {
return getDelegate().getBooleanValue(SECTION, "pretty_print", false);
}
@Value.Lazy
public Boolean executeRemotely() {
return getDelegate().getBooleanValue(SECTION, "execute_remotely", false);
}
private ToolProvider mkDistProviderFromTarget(UnconfiguredBuildTarget target) {
String source = String.format("[%s] %s", SECTION, DIST_FIELD);
return new InferDistFromTargetProvider(target, getDistBinary(), source);
}
private ToolProvider mkDistProviderFromPath(String path) {
String errorMessage = String.format("%s:%s path not found", SECTION, DIST_FIELD);
return new ConstantToolProvider(
new InferDistTool(
() -> getDelegate().getPathSourcePath(Paths.get(path), errorMessage), getDistBinary()));
}
}
| facebook/buck | src/com/facebook/buck/infer/InferConfig.java | Java | apache-2.0 | 4,497 |
/*
* Copyright 2016 The OpenYOLO Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openyolo.spi.assetlinks.data;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertTrue;
import java.util.List;
import org.json.JSONObject;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.valid4j.errors.RequireViolation;
/**
* Tests for {@link WebAssetStatementDeserializer}.
*/
@RunWith(RobolectricTestRunner.class)
@Config(manifest = Config.NONE)
public class WebAssetStatementDeserializerTest {
@Test(expected = RequireViolation.class)
public void testNullJson() {
new WebAssetStatementDeserializer().deserialize(null);
}
@Test
public void testNoTarget() {
JSONObject json = new JSONObject();
final List<WebSiteAssetStatement> assetStatements = new WebAssetStatementDeserializer()
.deserialize(json);
assertNotNull(assetStatements);
assertTrue(assetStatements.isEmpty());
}
}
| iainmcgin/OpenYOLO-Android | spi/javatests/java/org/openyolo/spi/assetlinks/data/WebAssetStatementDeserializerTest.java | Java | apache-2.0 | 1,656 |
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.xpack.security.authc.support.mapper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.message.ParameterizedMessage;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.support.ContextPreservingActionListener;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.CheckedBiConsumer;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.concurrent.ThreadContext;
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.script.ScriptService;
import org.elasticsearch.xpack.core.security.ScrollHelper;
import org.elasticsearch.xpack.core.security.action.realm.ClearRealmCacheAction;
import org.elasticsearch.xpack.core.security.action.realm.ClearRealmCacheRequest;
import org.elasticsearch.xpack.core.security.action.rolemapping.DeleteRoleMappingRequest;
import org.elasticsearch.xpack.core.security.action.rolemapping.PutRoleMappingRequest;
import org.elasticsearch.xpack.core.security.authc.support.mapper.ExpressionRoleMapping;
import org.elasticsearch.xpack.core.security.authc.support.mapper.expressiondsl.ExpressionModel;
import org.elasticsearch.xpack.core.security.index.RestrictedIndicesNames;
import org.elasticsearch.xpack.security.authc.support.CachingRealm;
import org.elasticsearch.xpack.security.authc.support.UserRoleMapper;
import org.elasticsearch.xpack.security.support.SecurityIndexManager;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import static org.elasticsearch.action.DocWriteResponse.Result.CREATED;
import static org.elasticsearch.action.DocWriteResponse.Result.DELETED;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.index.mapper.MapperService.SINGLE_MAPPING_NAME;
import static org.elasticsearch.search.SearchService.DEFAULT_KEEPALIVE_SETTING;
import static org.elasticsearch.xpack.core.ClientHelper.SECURITY_ORIGIN;
import static org.elasticsearch.xpack.core.ClientHelper.executeAsyncWithOrigin;
import static org.elasticsearch.xpack.core.security.index.RestrictedIndicesNames.SECURITY_MAIN_ALIAS;
import static org.elasticsearch.xpack.security.support.SecurityIndexManager.isIndexDeleted;
import static org.elasticsearch.xpack.security.support.SecurityIndexManager.isMoveFromRedToNonRed;
/**
* This store reads + writes {@link ExpressionRoleMapping role mappings} in an Elasticsearch
* {@link RestrictedIndicesNames#SECURITY_MAIN_ALIAS index}.
* <br>
* The store is responsible for all read and write operations as well as
* {@link #resolveRoles(UserData, ActionListener) resolving roles}.
* <p>
* No caching is done by this class, it is handled at a higher level and no polling for changes
* is done by this class. Modification operations make a best effort attempt to clear the cache
* on all nodes for the user that was modified.
*/
public class NativeRoleMappingStore implements UserRoleMapper {
private static final Logger logger = LogManager.getLogger(NativeRoleMappingStore.class);
static final String DOC_TYPE_FIELD = "doc_type";
static final String DOC_TYPE_ROLE_MAPPING = "role-mapping";
private static final String ID_PREFIX = DOC_TYPE_ROLE_MAPPING + "_";
private static final ActionListener<Object> NO_OP_ACTION_LISTENER = new ActionListener<Object>() {
@Override
public void onResponse(Object o) {
// nothing
}
@Override
public void onFailure(Exception e) {
// nothing
}
};
private final Settings settings;
private final Client client;
private final SecurityIndexManager securityIndex;
private final ScriptService scriptService;
private final List<String> realmsToRefresh = new CopyOnWriteArrayList<>();
public NativeRoleMappingStore(Settings settings, Client client, SecurityIndexManager securityIndex, ScriptService scriptService) {
this.settings = settings;
this.client = client;
this.securityIndex = securityIndex;
this.scriptService = scriptService;
}
private String getNameFromId(String id) {
assert id.startsWith(ID_PREFIX);
return id.substring(ID_PREFIX.length());
}
private String getIdForName(String name) {
return ID_PREFIX + name;
}
/**
* Loads all mappings from the index.
* <em>package private</em> for unit testing
*/
protected void loadMappings(ActionListener<List<ExpressionRoleMapping>> listener) {
if (securityIndex.isIndexUpToDate() == false) {
listener.onFailure(new IllegalStateException(
"Security index is not on the current version - the native realm will not be operational until " +
"the upgrade API is run on the security index"));
return;
}
final QueryBuilder query = QueryBuilders.termQuery(DOC_TYPE_FIELD, DOC_TYPE_ROLE_MAPPING);
final Supplier<ThreadContext.StoredContext> supplier = client.threadPool().getThreadContext().newRestorableContext(false);
try (ThreadContext.StoredContext ignore = client.threadPool().getThreadContext().stashWithOrigin(SECURITY_ORIGIN)) {
SearchRequest request = client.prepareSearch(SECURITY_MAIN_ALIAS)
.setScroll(DEFAULT_KEEPALIVE_SETTING.get(settings))
.setQuery(query)
.setSize(1000)
.setFetchSource(true)
.request();
request.indicesOptions().ignoreUnavailable();
ScrollHelper.fetchAllByEntity(client, request,
new ContextPreservingActionListener<>(supplier, ActionListener.wrap((Collection<ExpressionRoleMapping> mappings) ->
listener.onResponse(mappings.stream().filter(Objects::nonNull).collect(Collectors.toList())),
ex -> {
logger.error(new ParameterizedMessage("failed to load role mappings from index [{}] skipping all mappings.",
SECURITY_MAIN_ALIAS), ex);
listener.onResponse(Collections.emptyList());
})),
doc -> buildMapping(getNameFromId(doc.getId()), doc.getSourceRef()));
}
}
protected ExpressionRoleMapping buildMapping(String id, BytesReference source) {
try (InputStream stream = source.streamInput();
XContentParser parser = XContentType.JSON.xContent()
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) {
return ExpressionRoleMapping.parse(id, parser);
} catch (Exception e) {
logger.warn(new ParameterizedMessage("Role mapping [{}] cannot be parsed and will be skipped", id), e);
return null;
}
}
/**
* Stores (create or update) a single mapping in the index
*/
public void putRoleMapping(PutRoleMappingRequest request, ActionListener<Boolean> listener) {
modifyMapping(request.getName(), this::innerPutMapping, request, listener);
}
/**
* Deletes a named mapping from the index
*/
public void deleteRoleMapping(DeleteRoleMappingRequest request, ActionListener<Boolean> listener) {
modifyMapping(request.getName(), this::innerDeleteMapping, request, listener);
}
private <Request, Result> void modifyMapping(String name, CheckedBiConsumer<Request, ActionListener<Result>, Exception> inner,
Request request, ActionListener<Result> listener) {
if (securityIndex.isIndexUpToDate() == false) {
listener.onFailure(new IllegalStateException(
"Security index is not on the current version - the native realm will not be operational until " +
"the upgrade API is run on the security index"));
} else {
try {
inner.accept(request, ActionListener.wrap(r -> refreshRealms(listener, r), listener::onFailure));
} catch (Exception e) {
logger.error(new ParameterizedMessage("failed to modify role-mapping [{}]", name), e);
listener.onFailure(e);
}
}
}
private void innerPutMapping(PutRoleMappingRequest request, ActionListener<Boolean> listener) {
final ExpressionRoleMapping mapping = request.getMapping();
securityIndex.prepareIndexIfNeededThenExecute(listener::onFailure, () -> {
final XContentBuilder xContentBuilder;
try {
xContentBuilder = mapping.toXContent(jsonBuilder(), ToXContent.EMPTY_PARAMS, true);
} catch (IOException e) {
listener.onFailure(e);
return;
}
executeAsyncWithOrigin(client.threadPool().getThreadContext(), SECURITY_ORIGIN,
client.prepareIndex(SECURITY_MAIN_ALIAS, SINGLE_MAPPING_NAME, getIdForName(mapping.getName()))
.setSource(xContentBuilder)
.setRefreshPolicy(request.getRefreshPolicy())
.request(),
new ActionListener<IndexResponse>() {
@Override
public void onResponse(IndexResponse indexResponse) {
boolean created = indexResponse.getResult() == CREATED;
listener.onResponse(created);
}
@Override
public void onFailure(Exception e) {
logger.error(new ParameterizedMessage("failed to put role-mapping [{}]", mapping.getName()), e);
listener.onFailure(e);
}
}, client::index);
});
}
private void innerDeleteMapping(DeleteRoleMappingRequest request, ActionListener<Boolean> listener) {
final SecurityIndexManager frozenSecurityIndex = securityIndex.freeze();
if (frozenSecurityIndex.indexExists() == false) {
listener.onResponse(false);
} else if (securityIndex.isAvailable() == false) {
listener.onFailure(frozenSecurityIndex.getUnavailableReason());
} else {
securityIndex.checkIndexVersionThenExecute(listener::onFailure, () -> {
executeAsyncWithOrigin(client.threadPool().getThreadContext(), SECURITY_ORIGIN,
client.prepareDelete(SECURITY_MAIN_ALIAS, SINGLE_MAPPING_NAME, getIdForName(request.getName()))
.setRefreshPolicy(request.getRefreshPolicy())
.request(),
new ActionListener<DeleteResponse>() {
@Override
public void onResponse(DeleteResponse deleteResponse) {
boolean deleted = deleteResponse.getResult() == DELETED;
listener.onResponse(deleted);
}
@Override
public void onFailure(Exception e) {
logger.error(new ParameterizedMessage("failed to delete role-mapping [{}]", request.getName()), e);
listener.onFailure(e);
}
}, client::delete);
});
}
}
/**
* Retrieves one or more mappings from the index.
* If <code>names</code> is <code>null</code> or {@link Set#isEmpty empty}, then this retrieves all mappings.
* Otherwise it retrieves the specified mappings by name.
*/
public void getRoleMappings(Set<String> names, ActionListener<List<ExpressionRoleMapping>> listener) {
if (names == null || names.isEmpty()) {
getMappings(listener);
} else {
getMappings(new ActionListener<List<ExpressionRoleMapping>>() {
@Override
public void onResponse(List<ExpressionRoleMapping> mappings) {
final List<ExpressionRoleMapping> filtered = mappings.stream()
.filter(m -> names.contains(m.getName()))
.collect(Collectors.toList());
listener.onResponse(filtered);
}
@Override
public void onFailure(Exception e) {
listener.onFailure(e);
}
});
}
}
private void getMappings(ActionListener<List<ExpressionRoleMapping>> listener) {
if (securityIndex.isAvailable()) {
loadMappings(listener);
} else {
logger.info("The security index is not yet available - no role mappings can be loaded");
if (logger.isDebugEnabled()) {
logger.debug("Security Index [{}] [exists: {}] [available: {}] [mapping up to date: {}]",
SECURITY_MAIN_ALIAS,
securityIndex.indexExists(),
securityIndex.isAvailable(),
securityIndex.isMappingUpToDate()
);
}
listener.onResponse(Collections.emptyList());
}
}
/**
* Provides usage statistics for this store.
* The resulting map contains the keys
* <ul>
* <li><code>size</code> - The total number of mappings stored in the index</li>
* <li><code>enabled</code> - The number of mappings that are
* {@link ExpressionRoleMapping#isEnabled() enabled}</li>
* </ul>
*/
public void usageStats(ActionListener<Map<String, Object>> listener) {
if (securityIndex.isAvailable() == false) {
reportStats(listener, Collections.emptyList());
} else {
getMappings(ActionListener.wrap(mappings -> reportStats(listener, mappings), listener::onFailure));
}
}
private void reportStats(ActionListener<Map<String, Object>> listener, List<ExpressionRoleMapping> mappings) {
Map<String, Object> usageStats = new HashMap<>();
usageStats.put("size", mappings.size());
usageStats.put("enabled", mappings.stream().filter(ExpressionRoleMapping::isEnabled).count());
listener.onResponse(usageStats);
}
public void onSecurityIndexStateChange(SecurityIndexManager.State previousState, SecurityIndexManager.State currentState) {
if (isMoveFromRedToNonRed(previousState, currentState) || isIndexDeleted(previousState, currentState) ||
previousState.isIndexUpToDate != currentState.isIndexUpToDate) {
refreshRealms(NO_OP_ACTION_LISTENER, null);
}
}
private <Result> void refreshRealms(ActionListener<Result> listener, Result result) {
if (realmsToRefresh.isEmpty()) {
listener.onResponse(result);
return;
}
final String[] realmNames = this.realmsToRefresh.toArray(Strings.EMPTY_ARRAY);
executeAsyncWithOrigin(client, SECURITY_ORIGIN, ClearRealmCacheAction.INSTANCE, new ClearRealmCacheRequest().realms(realmNames),
ActionListener.wrap(
response -> {
logger.debug((org.apache.logging.log4j.util.Supplier<?>) () -> new ParameterizedMessage(
"Cleared cached in realms [{}] due to role mapping change", Arrays.toString(realmNames)));
listener.onResponse(result);
},
ex -> {
logger.warn(new ParameterizedMessage("Failed to clear cache for realms [{}]", Arrays.toString(realmNames)), ex);
listener.onFailure(ex);
}));
}
@Override
public void resolveRoles(UserData user, ActionListener<Set<String>> listener) {
getRoleMappings(null, ActionListener.wrap(
mappings -> {
final ExpressionModel model = user.asModel();
final Set<String> roles = mappings.stream()
.filter(ExpressionRoleMapping::isEnabled)
.filter(m -> m.getExpression().match(model))
.flatMap(m -> {
final Set<String> roleNames = m.getRoleNames(scriptService, model);
logger.trace("Applying role-mapping [{}] to user-model [{}] produced role-names [{}]",
m.getName(), model, roleNames);
return roleNames.stream();
})
.collect(Collectors.toSet());
logger.debug("Mapping user [{}] to roles [{}]", user, roles);
listener.onResponse(roles);
}, listener::onFailure
));
}
/**
* Indicates that the provided realm should have its cache cleared if this store is updated
* (that is, {@link #putRoleMapping(PutRoleMappingRequest, ActionListener)} or
* {@link #deleteRoleMapping(DeleteRoleMappingRequest, ActionListener)} are called).
* @see ClearRealmCacheAction
*/
@Override
public void refreshRealmOnChange(CachingRealm realm) {
realmsToRefresh.add(realm.name());
}
}
| coding0011/elasticsearch | x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/support/mapper/NativeRoleMappingStore.java | Java | apache-2.0 | 18,616 |
/*
* Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved.
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
/*
* Copyright 2004,2005 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.sun.org.apache.xerces.internal.impl.dv.xs;
import java.math.BigInteger;
import javax.xml.datatype.DatatypeConstants;
import javax.xml.datatype.Duration;
import com.sun.org.apache.xerces.internal.impl.dv.InvalidDatatypeValueException;
import com.sun.org.apache.xerces.internal.impl.dv.ValidationContext;
/**
* Used to validate the <yearMonthDuration> type
*
* @xerces.internal
*
* @author Ankit Pasricha, IBM
*
* @version $Id: YearMonthDurationDV.java,v 1.6 2010-11-01 04:39:47 joehw Exp $
*/
class YearMonthDurationDV extends DurationDV {
public Object getActualValue(String content, ValidationContext context)
throws InvalidDatatypeValueException {
try {
return parse(content, DurationDV.YEARMONTHDURATION_TYPE);
}
catch (Exception ex) {
throw new InvalidDatatypeValueException("cvc-datatype-valid.1.2.1", new Object[]{content, "yearMonthDuration"});
}
}
protected Duration getDuration(DateTimeData date) {
int sign = 1;
if ( date.year<0 || date.month<0) {
sign = -1;
}
return datatypeFactory.newDuration(sign == 1,
date.year != DatatypeConstants.FIELD_UNDEFINED?BigInteger.valueOf(sign*date.year):null,
date.month != DatatypeConstants.FIELD_UNDEFINED?BigInteger.valueOf(sign*date.month):null,
null,
null,
null,
null);
}
}
| shun634501730/java_source_cn | src_en/com/sun/org/apache/xerces/internal/impl/dv/xs/YearMonthDurationDV.java | Java | apache-2.0 | 2,247 |
/*
* Copyright 2014 Space Dynamics Laboratory - Utah State University Research Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package edu.usu.sdl.openstorefront.web.test.system;
import edu.usu.sdl.openstorefront.core.api.LookupService;
import edu.usu.sdl.openstorefront.core.api.model.AsyncTaskCallback;
import edu.usu.sdl.openstorefront.core.api.model.TaskFuture;
import edu.usu.sdl.openstorefront.core.api.model.TaskRequest;
import edu.usu.sdl.openstorefront.core.entity.ErrorTypeCode;
import edu.usu.sdl.openstorefront.service.manager.AsyncTaskManager;
import edu.usu.sdl.openstorefront.web.test.BaseTestCase;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
/**
*
* @author dshurtleff
*/
public class AsyncProxyTest
extends BaseTestCase
{
private List<ErrorTypeCode> errorTypeCodes = new ArrayList<>();
public AsyncProxyTest()
{
this.description = "Async Proxy Test";
}
@Override
protected void runInternalTest()
{
results.append("Call back style: <br>");
TaskRequest taskRequest = new TaskRequest();
taskRequest.setAllowMultiple(true);
taskRequest.setName(UUID.randomUUID().toString());
taskRequest.setCallback(new AsyncTaskCallback()
{
@Override
public void beforeExecute(TaskFuture taskFuture)
{
}
@Override
public void afterExecute(TaskFuture taskFuture)
{
try {
results.append("Runnning in callback: <br>");
List<ErrorTypeCode> errorTypeCodesLocal = (List<ErrorTypeCode>) taskFuture.getFuture().get();
errorTypeCodesLocal.forEach(code -> {
results.append(code.getCode()).append(" - ").append(code.getDescription()).append("<br>");
});
} catch (InterruptedException | ExecutionException ex) {
throw new RuntimeException(ex);
}
}
});
LookupService asyncLookup = service.getAsyncProxy(service.getLookupService(), taskRequest);
asyncLookup.findLookup(ErrorTypeCode.class);
results.append("Lookup style: <br>");
TaskFuture taskFuture = AsyncTaskManager.getTaskByName(taskRequest.getName());
try {
errorTypeCodes = (List<ErrorTypeCode>) taskFuture.getFuture().get();
errorTypeCodes.forEach(code -> {
results.append(code.getCode()).append(" - ").append(code.getDescription()).append("<br>");
});
Thread.sleep(100);
} catch (InterruptedException | ExecutionException ex) {
throw new RuntimeException(ex);
}
}
}
| tyler-travis/openstorefront | server/openstorefront/openstorefront-web/src/main/java/edu/usu/sdl/openstorefront/web/test/system/AsyncProxyTest.java | Java | apache-2.0 | 2,955 |
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ignite.internal.processors.cache.transactions;
import java.util.Collection;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCheckedException;
import org.junit.Ignore;
/** */
public class AtomicVolatilePartitionCounterStateConsistencyTest extends AtomicPartitionCounterStateConsistencyTest {
/** {@inheritDoc} */
@Override protected boolean persistenceEnabled() {
return false;
}
/** {@inheritDoc} */
@Override protected int partitions() {
return 1024;
}
/** {@inheritDoc} */
@Ignore
@Override public void testSingleThreadedUpdateOrder() throws Exception {
// Not applicable for volatile mode.
}
/** {@inheritDoc} */
@Ignore
@Override public void testPartitionConsistencyCancelledRebalanceCoordinatorIsDemander() throws Exception {
// Not applicable for volatile mode.
}
/** {@inheritDoc} */
@Ignore
@Override public void testLateAffinityChangeDuringExchange() throws Exception {
// Not applicable for volatile mode.
}
/** {@inheritDoc} */
@Override protected void forceCheckpoint(Collection<Ignite> nodes) throws IgniteCheckedException {
// No-op.
}
}
| samaitra/ignite | modules/core/src/test/java/org/apache/ignite/internal/processors/cache/transactions/AtomicVolatilePartitionCounterStateConsistencyTest.java | Java | apache-2.0 | 2,030 |
/*
* Copyright (c) 2010-2012 Grid Dynamics Consulting Services, Inc, All Rights Reserved
* http://www.griddynamics.com
*
* This library is free software; you can redistribute it and/or modify it under the terms of
* the Apache License; either
* version 2.0 of the License, or any later version.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.griddynamics.jagger.dbapi.parameter;
import com.google.common.base.Objects;
public class GroupKey {
private String upperName;
private String leftName;
public GroupKey(String upperName) {
this.upperName = upperName;
this.leftName = upperName;
}
public GroupKey(String upperName, String leftName) {
this.upperName = upperName;
this.leftName = leftName;
}
public String getUpperName() {
return upperName;
}
public String getLeftName() {
return leftName;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
GroupKey groupKey = (GroupKey) o;
if (leftName != null ? !leftName.equals(groupKey.leftName) : groupKey.leftName != null) return false;
if (upperName != null ? !upperName.equals(groupKey.upperName) : groupKey.upperName != null) return false;
return true;
}
@Override
public int hashCode() {
int result = upperName != null ? upperName.hashCode() : 0;
result = 31 * result + (leftName != null ? leftName.hashCode() : 0);
return result;
}
@Override
public String toString() {
return Objects.toStringHelper(this)
.add("upperName", upperName)
.add("leftName", leftName)
.toString();
}
}
| Nmishin/jagger | dbapi/src/main/java/com/griddynamics/jagger/dbapi/parameter/GroupKey.java | Java | apache-2.0 | 2,536 |
/**
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.activemq.openwire.v4;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import org.apache.activemq.openwire.*;
import org.apache.activemq.command.*;
/**
* Test case for the OpenWire marshalling for RemoveSubscriptionInfo
*
*
* NOTE!: This file is auto generated - do not modify!
* if you need to make a change, please see the modify the groovy scripts in the
* under src/gram/script and then use maven openwire:generate to regenerate
* this file.
*
*
*/
public class RemoveSubscriptionInfoTest extends BaseCommandTestSupport {
public static RemoveSubscriptionInfoTest SINGLETON = new RemoveSubscriptionInfoTest();
public Object createObject() throws Exception {
RemoveSubscriptionInfo info = new RemoveSubscriptionInfo();
populateObject(info);
return info;
}
protected void populateObject(Object object) throws Exception {
super.populateObject(object);
RemoveSubscriptionInfo info = (RemoveSubscriptionInfo) object;
info.setConnectionId(createConnectionId("ConnectionId:1"));
info.setSubscriptionName("SubcriptionName:2");
info.setClientId("ClientId:3");
}
}
| ryanemerson/activemq-artemis | tests/activemq5-unit-tests/src/test/java/org/apache/activemq/openwire/v4/RemoveSubscriptionInfoTest.java | Java | apache-2.0 | 2,049 |
/*
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cache.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* {@code @CacheConfig} provides a mechanism for sharing common cache-related
* settings at the class level.
*
* <p>When this annotation is present on a given class, it provides a set
* of default settings for any cache operation defined in that class.
*
* @author Stephane Nicoll
* @author Sam Brannen
* @since 4.1
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface CacheConfig {
/**
* Names of the default caches to consider for caching operations defined
* in the annotated class.
* <p>If none is set at the operation level, these are used instead of the default.
* <p>May be used to determine the target cache (or caches), matching the
* qualifier value or the bean names of a specific bean definition.
*/
String[] cacheNames() default {};
/**
* The bean name of the default {@link org.springframework.cache.interceptor.KeyGenerator} to
* use for the class.
* <p>If none is set at the operation level, this one is used instead of the default.
* <p>The key generator is mutually exclusive with the use of a custom key. When such key is
* defined for the operation, the value of this key generator is ignored.
*/
String keyGenerator() default "";
/**
* The bean name of the custom {@link org.springframework.cache.CacheManager} to use to
* create a default {@link org.springframework.cache.interceptor.CacheResolver} if none
* is set already.
* <p>If no resolver and no cache manager are set at the operation level, and no cache
* resolver is set via {@link #cacheResolver}, this one is used instead of the default.
* @see org.springframework.cache.interceptor.SimpleCacheResolver
*/
String cacheManager() default "";
/**
* The bean name of the custom {@link org.springframework.cache.interceptor.CacheResolver} to use.
* <p>If no resolver and no cache manager are set at the operation level, this one is used
* instead of the default.
*/
String cacheResolver() default "";
}
| shivpun/spring-framework | spring-context/src/main/java/org/springframework/cache/annotation/CacheConfig.java | Java | apache-2.0 | 2,865 |
/*
* Copyright 2014 Avanza Bank AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.avanza.astrix.beans.ft;
import com.avanza.astrix.beans.core.AstrixBeanKey;
import com.avanza.astrix.beans.core.BeanProxy;
import com.avanza.astrix.beans.service.ServiceBeanProxyFactory;
/**
*
* @author Elias Lindholm
*
*/
final class FaultToleranceServiceBeanProxyFactory implements ServiceBeanProxyFactory {
private final BeanFaultToleranceFactory ftFactory;
public FaultToleranceServiceBeanProxyFactory(BeanFaultToleranceFactory ftFactory) {
this.ftFactory = ftFactory;
}
@Override
public BeanProxy create(AstrixBeanKey<?> beanKey) {
return ftFactory.createFaultToleranceProxy(beanKey);
}
@Override
public int order() {
return 1;
}
}
| jensim/astrix | astrix-context/src/main/java/com/avanza/astrix/beans/ft/FaultToleranceServiceBeanProxyFactory.java | Java | apache-2.0 | 1,277 |
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
package org.apache.harmony.lang.management.tests.java.lang.management;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.MemoryUsage;
import junit.framework.TestCase;
public class MemoryMXBeanTest extends TestCase {
private MemoryMXBean mb;
protected void setUp() throws Exception {
super.setUp();
mb = ManagementFactory.getMemoryMXBean();
assertNotNull(mb);
}
protected void tearDown() throws Exception {
super.tearDown();
}
/*
* Test method for 'java.lang.management.MemoryMXBean.getHeapMemoryUsage()'
*/
public void testGetHeapMemoryUsage() {
MemoryUsage mu = mb.getHeapMemoryUsage();
assertNotNull(mu);
assertTrue(mu.getCommitted() >= mu.getUsed());
assertTrue(mu.getCommitted() <= mu.getMax());
assertTrue(mu.getUsed() <= mu.getMax());
}
/*
* Test method for 'java.lang.management.MemoryMXBean.getNonHeapMemoryUsage()'
*/
public void testGetNonHeapMemoryUsage() {
MemoryUsage mu = mb.getNonHeapMemoryUsage();
assertNotNull(mu);
assertTrue(mu.getCommitted() >= mu.getUsed());
if (mu.getMax() != -1) {
// If max is defined then used and committed will always
// be less than or equal to it
assertTrue(mu.getCommitted() <= mu.getMax());
assertTrue(mu.getUsed() <= mu.getMax());
}
}
/*
* Test method for 'java.lang.management.MemoryMXBean.getObjectPendingFinalizationCount()'
*/
public void testGetObjectPendingFinalizationCount() {
assertTrue(mb.getObjectPendingFinalizationCount() > -1);
}
/*
* Test method for 'java.lang.management.MemoryMXBean.setVerbose(boolean)'
*/
public void testSetVerbose() {
boolean initialVal = mb.isVerbose();
mb.setVerbose(!initialVal);
assertTrue(mb.isVerbose() != initialVal);
mb.setVerbose(initialVal);
assertTrue(mb.isVerbose() == initialVal);
}
}
| freeVM/freeVM | enhanced/java/classlib/modules/lang-management/src/test/api/java/org/apache/harmony/lang/management/tests/java/lang/management/MemoryMXBeanTest.java | Java | apache-2.0 | 2,895 |
/*
* Copyright (C) 2010 Toni Menzel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.ops4j.pax.exam.container.remote;
import static org.ops4j.pax.exam.OptionUtils.filter;
import org.ops4j.pax.exam.Option;
import org.ops4j.pax.exam.container.remote.options.RBCLookupTimeoutOption;
import org.ops4j.pax.exam.container.remote.options.RBCPortOption;
/**
* Minimal parser for the rbcremote fragment.
*/
public class Parser {
private String host;
private Integer port;
private long timeout;
public Parser(Option[] options) {
extractArguments(filter(RBCPortOption.class, options));
extractArguments(filter(RBCLookupTimeoutOption.class, options));
assert port != null : "Port should never be null.";
assert host != null : "Host should never be null.";
}
private void extractArguments(RBCLookupTimeoutOption[] options) {
for (RBCLookupTimeoutOption op : options) {
timeout = op.getTimeout();
}
}
private void extractArguments(RBCPortOption[] rbcPortOptions) {
for (RBCPortOption op : rbcPortOptions) {
host = op.getHost();
port = op.getPort();
}
}
public String getHost() {
return host;
}
public Integer getRMIPort() {
return port;
}
public long getRMILookupTimpout() {
return timeout;
}
public Integer getPort() {
return port;
}
}
| ops4j/org.ops4j.pax.exam2 | containers/pax-exam-container-remote/src/main/java/org/ops4j/pax/exam/container/remote/Parser.java | Java | apache-2.0 | 1,966 |
/*
* Copyright (c) 2012, salesforce.com, inc.
* All rights reserved.
* Redistribution and use of this software in source and binary forms, with or
* without modification, are permitted provided that the following conditions
* are met:
* - Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither the name of salesforce.com, inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission of salesforce.com, inc.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package com.salesforce.androidsdk.smartstore.store;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.salesforce.androidsdk.smartstore.store.SmartStore.Type;
/**
* Simple class to represent index spec
*/
public class IndexSpec {
public final String path;
public final Type type;
public final String columnName;
public IndexSpec(String path, Type type) {
this.path = path;
this.type = type;
this.columnName = null; // undefined
}
public IndexSpec(String path, Type type, String columnName) {
this.path = path;
this.type = type;
this.columnName = columnName;
}
@Override
public int hashCode() {
int result = 17;
result = 31 * result + path.hashCode();
result = 31 * result + type.hashCode();
if (columnName != null)
result = 31 * result + columnName.hashCode();
return result;
}
@Override
public boolean equals(Object obj) {
if (obj == null)
return false;
if (obj == this)
return true;
if (!(obj instanceof IndexSpec))
return false;
IndexSpec rhs = (IndexSpec) obj;
boolean result = true;
result = result && path.equals(rhs.path);
result = result && type.equals(rhs.type);
if (columnName == null)
result = result && (columnName == rhs.columnName);
else
result = result && columnName.equals(rhs.columnName);
return result;
}
/**
* @return path | type
*/
public String getPathType() {
return path + "|" + type;
}
/**
* @return JSONObject for this IndexSpec
* @throws JSONException
*/
public JSONObject toJSON() throws JSONException {
JSONObject json = new JSONObject();
json.put("path", path);
json.put("type", type);
json.put("columnName", columnName);
return json;
}
/**
* @param indexSpecs
* @return JSONArray for the array of IndexSpec's
* @throws JSONException
*/
public static JSONArray toJSON(IndexSpec[] indexSpecs) throws JSONException {
JSONArray json = new JSONArray();
for(IndexSpec indexSpec : indexSpecs) {
json.put(indexSpec.toJSON());
}
return json;
}
/**
* @param jsonArray
* @return IndexSpec[] from a JSONArray
* @throws JSONException
*/
public static IndexSpec[] fromJSON(JSONArray jsonArray) throws JSONException {
List<IndexSpec> list = new ArrayList<IndexSpec>();
for(int i=0; i<jsonArray.length(); i++) {
list.add(IndexSpec.fromJSON(jsonArray.getJSONObject(i)));
}
return list.toArray(new IndexSpec[0]);
}
/**
* Return IndexSpec given JSONObject
* @param json
* @return
* @throws JSONException
*/
public static IndexSpec fromJSON(JSONObject json) throws JSONException {
return new IndexSpec(json.getString("path"), Type.valueOf(json.getString("type")), json.optString("columnName"));
}
/**
* @param indexSpecs
* @return map index spec path to index spec
*/
public static Map<String, IndexSpec> mapForIndexSpecs(IndexSpec[] indexSpecs) {
Map<String, IndexSpec> map = new HashMap<String, IndexSpec>();
for (IndexSpec indexSpec : indexSpecs) {
map.put(indexSpec.path, indexSpec);
}
return map;
}
/**
* @param indexSpecs
* @return true if at least one of the indexSpec is of type full_text
*/
public static boolean hasFTS(IndexSpec[] indexSpecs) {
for (IndexSpec indexSpec : indexSpecs) {
if (indexSpec.type == Type.full_text) {
return true;
}
}
return false;
}
} | huminzhi/SalesforceMobileSDK-Android | libs/SmartStore/src/com/salesforce/androidsdk/smartstore/store/IndexSpec.java | Java | apache-2.0 | 5,296 |
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.nifi.registry.ui;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import io.github.bonigarcia.wdm.WebDriverManager;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
public class ITCreateDuplicateBucket {
private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private WebDriverWait wait;
private StringBuffer verificationErrors = new StringBuffer();
@Before
public void setUp() throws Exception {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
baseUrl = "http://localhost:18080/nifi-registry";
wait = new WebDriverWait(driver, 30);
}
@Test
public void testCreateDuplicateBucket() throws Exception {
// go directly to settings by URL
driver.get(baseUrl + "/#/administration/workflow");
// wait for administration route to load
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("[data-automation-id='no-buckets-message']")));
// confirm new bucket button exists
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("[data-automation-id='new-bucket-button']")));
// select new bucket button
WebElement newBucketButton = driver.findElement(By.cssSelector("[data-automation-id='new-bucket-button']"));
newBucketButton.click();
// wait for new bucket dialog
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#nifi-registry-admin-create-bucket-dialog")));
// confirm bucket name field exists
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#nifi-registry-admin-create-bucket-dialog input")));
// place cursor in bucket name field
WebElement bucketNameInput = driver.findElement(By.cssSelector("#nifi-registry-admin-create-bucket-dialog input"));
bucketNameInput.clear();
// name the bucket ABC
bucketNameInput.sendKeys("ABC");
// confirm create bucket button exists
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("[data-automation-id='create-new-bucket-button']")));
// select create bucket button
WebElement createNewBucketButton = driver.findElement(By.cssSelector("[data-automation-id='create-new-bucket-button']"));
createNewBucketButton.click();
// wait for create bucket dialog to close
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector("#nifi-registry-admin-create-bucket-dialog")));
// verify bucket added
List<WebElement> bucketCount = driver.findElements(By.cssSelector("#nifi-registry-workflow-administration-buckets-list-container > div"));
assertEquals(1, bucketCount.size());
// confirm new bucket button exists
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("[data-automation-id='new-bucket-button']")));
// select new bucket button
newBucketButton = driver.findElement(By.cssSelector("[data-automation-id='new-bucket-button']"));
newBucketButton.click();
// wait for new bucket dialog
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#nifi-registry-admin-create-bucket-dialog")));
// confirm bucket name field exists
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#nifi-registry-admin-create-bucket-dialog input")));
// place cursor in bucket name field
bucketNameInput = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#nifi-registry-admin-create-bucket-dialog input")));
bucketNameInput.clear();
// name the bucket ABC again
bucketNameInput.sendKeys("ABC");
// confirm create bucket button exists
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("[data-automation-id='create-new-bucket-button']")));
// select create bucket button
createNewBucketButton = driver.findElement(By.cssSelector("[data-automation-id='create-new-bucket-button']"));
createNewBucketButton.click();
// wait for the new bucket dialog to close
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector("#nifi-registry-admin-create-bucket-dialog")));
// wait for error dialog
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.cdk-overlay-pane")));
// confirm the duplicate bucket error
WebElement selectOKButton = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.cdk-overlay-pane")));
Actions actions = new Actions(driver);
actions.moveToElement(selectOKButton).click().build().perform();
// wait for the confirm dialog to close
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector("div.cdk-overlay-pane")));
// verify bucket ABC still there
bucketCount = driver.findElements(By.cssSelector("#nifi-registry-workflow-administration-buckets-list-container > div"));
assertEquals(1, bucketCount.size());
}
@After
public void tearDown() throws Exception {
// bucket cleanup
// confirm all buckets checkbox exists
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#nifi-registry-workflow-administration-buckets-list-container-column-header div.mat-checkbox-inner-container")));
// select all buckets checkbox
WebElement selectAllCheckbox = driver.findElement(By.cssSelector("#nifi-registry-workflow-administration-buckets-list-container-column-header div.mat-checkbox-inner-container"));
selectAllCheckbox.click();
// confirm actions drop down menu exists
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#nifi-registry-workflow-administration-perspective-buckets-container button.mat-fds-primary")));
// select actions drop down
WebElement selectActions = driver.findElement(By.cssSelector("#nifi-registry-workflow-administration-perspective-buckets-container button.mat-fds-primary"));
selectActions.click();
// select delete
WebElement selectDeleteBucket = driver.findElement(By.cssSelector("div.mat-menu-content button.mat-menu-item"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", selectDeleteBucket);
// verify bucket deleted
WebElement confirmDelete = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.fds-dialog-actions button.mat-fds-warn")));
confirmDelete.click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("[data-automation-id='no-buckets-message']")));
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
private boolean isAlertPresent() {
try {
driver.switchTo().alert();
return true;
} catch (NoAlertPresentException e) {
return false;
}
}
private String closeAlertAndGetItsText() {
try {
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
if (acceptNextAlert) {
alert.accept();
} else {
alert.dismiss();
}
return alertText;
} finally {
acceptNextAlert = true;
}
}
} | MikeThomsen/nifi | nifi-registry/nifi-registry-core/nifi-registry-web-ui/src/test/java/org/apache/nifi/registry/ui/ITCreateDuplicateBucket.java | Java | apache-2.0 | 9,295 |
/*
* Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.wso2.siddhi.core.util.extension.holder;
import org.wso2.siddhi.core.config.ExecutionPlanContext;
import org.wso2.siddhi.core.query.processor.stream.window.WindowProcessor;
public class WindowProcessorExtensionHolder extends AbstractExtensionHolder {
private static WindowProcessorExtensionHolder instance;
private WindowProcessorExtensionHolder(ExecutionPlanContext executionPlanContext) {
super(WindowProcessor.class, executionPlanContext);
}
public static WindowProcessorExtensionHolder getInstance(ExecutionPlanContext executionPlanContext) {
if (instance == null) {
instance = new WindowProcessorExtensionHolder(executionPlanContext);
}
return instance;
}
}
| keizer619/siddhi | modules/siddhi-core/src/main/java/org/wso2/siddhi/core/util/extension/holder/WindowProcessorExtensionHolder.java | Java | apache-2.0 | 1,383 |
package com.vaadin.tests.elements.abstracttextfield;
import com.vaadin.server.VaadinRequest;
import com.vaadin.tests.components.AbstractTestUI;
import com.vaadin.ui.AbstractField;
import com.vaadin.ui.AbstractMultiSelect;
import com.vaadin.ui.AbstractSingleSelect;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.CheckBoxGroup;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.DateField;
import com.vaadin.ui.ListSelect;
import com.vaadin.ui.NativeSelect;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.RadioButtonGroup;
import com.vaadin.ui.RichTextArea;
import com.vaadin.ui.Slider;
import com.vaadin.ui.TextArea;
import com.vaadin.ui.TextField;
import com.vaadin.ui.TwinColSelect;
public class AbstractFieldElementSetValueReadOnly extends AbstractTestUI {
private AbstractField<?>[] fields = { new TextArea(), new TextField(),
new DateField(), new PasswordField(), new CheckBox(),
new RichTextArea(), new Slider() };
private AbstractMultiSelect<?>[] multiSelects = { new ListSelect(),
new CheckBoxGroup(), new TwinColSelect() };
private AbstractSingleSelect<?>[] singleSelects = { new ComboBox(),
new NativeSelect(), new RadioButtonGroup() };
@Override
protected void setup(VaadinRequest request) {
for (AbstractField field : fields) {
field.setReadOnly(true);
addComponent(field);
}
for (AbstractMultiSelect multiSelect : multiSelects) {
multiSelect.setReadOnly(true);
addComponent(multiSelect);
}
for (AbstractSingleSelect singleSelect : singleSelects) {
singleSelect.setReadOnly(true);
addComponent(singleSelect);
}
}
@Override
protected String getTestDescription() {
return "When vaadin element is set ReadOnly, setValue() method should raise an exception";
}
@Override
protected Integer getTicketNumber() {
return 14068;
}
}
| peterl1084/framework | uitest/src/main/java/com/vaadin/tests/elements/abstracttextfield/AbstractFieldElementSetValueReadOnly.java | Java | apache-2.0 | 1,989 |
/*
* Copyright 2017-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package com.facebook.buck.rules.query;
import com.facebook.buck.query.CachingQueryEvaluator;
import com.facebook.buck.query.QueryEvaluator;
import com.facebook.buck.query.QueryException;
import com.facebook.buck.query.QueryExpression;
import com.facebook.buck.rules.TargetGraph;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import java.util.Objects;
import java.util.concurrent.ExecutionException;
/** Cache that evaluates and stores the result of a dependency {@link Query}. */
public class QueryCache {
private final LoadingCache<TargetGraph, CachingQueryEvaluator> evaluators;
public QueryCache() {
evaluators = CacheBuilder.newBuilder().build(CacheLoader.from(CachingQueryEvaluator::new));
}
QueryEvaluator getQueryEvaluator(TargetGraph targetGraph) {
try {
return evaluators.get(targetGraph);
} catch (ExecutionException e) {
throw new RuntimeException("Failed to obtain query evaluator", e);
}
}
@VisibleForTesting
boolean isPresent(TargetGraph targetGraph, GraphEnhancementQueryEnvironment env, Query query)
throws ExecutionException, QueryException {
CachingQueryEvaluator evaluator = evaluators.getIfPresent(targetGraph);
return Objects.nonNull(evaluator)
&& evaluator.isPresent(QueryExpression.parse(query.getQuery(), env));
}
}
| dsyang/buck | src/com/facebook/buck/rules/query/QueryCache.java | Java | apache-2.0 | 2,062 |
/*
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
package org.wso2.andes.server.management;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.security.AccessControlContext;
import java.security.AccessController;
import java.util.Set;
import javax.management.Attribute;
import javax.management.JMException;
import javax.management.MBeanInfo;
import javax.management.MBeanOperationInfo;
import javax.management.MBeanServer;
import javax.management.Notification;
import javax.management.NotificationListener;
import javax.management.ObjectName;
import javax.management.remote.JMXConnectionNotification;
import javax.management.remote.JMXPrincipal;
import javax.management.remote.MBeanServerForwarder;
import javax.security.auth.Subject;
import org.apache.log4j.Logger;
import org.wso2.andes.server.logging.actors.ManagementActor;
import org.wso2.andes.server.logging.messages.ManagementConsoleMessages;
import org.wso2.andes.server.registry.ApplicationRegistry;
import org.wso2.andes.server.security.SecurityManager;
import org.wso2.andes.server.security.access.Operation;
/**
* This class can be used by the JMXConnectorServer as an InvocationHandler for the mbean operations. It delegates
* JMX access decisions to the SecurityPlugin.
*/
public class MBeanInvocationHandlerImpl implements InvocationHandler, NotificationListener
{
private static final Logger _logger = Logger.getLogger(MBeanInvocationHandlerImpl.class);
private final static String DELEGATE = "JMImplementation:type=MBeanServerDelegate";
private MBeanServer _mbs;
private static ManagementActor _logActor;
public static MBeanServerForwarder newProxyInstance()
{
final InvocationHandler handler = new MBeanInvocationHandlerImpl();
final Class<?>[] interfaces = new Class[] { MBeanServerForwarder.class };
_logActor = new ManagementActor(ApplicationRegistry.getInstance().getRootMessageLogger());
Object proxy = Proxy.newProxyInstance(MBeanServerForwarder.class.getClassLoader(), interfaces, handler);
return MBeanServerForwarder.class.cast(proxy);
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
{
final String methodName = getMethodName(method, args);
if (methodName.equals("getMBeanServer"))
{
return _mbs;
}
if (methodName.equals("setMBeanServer"))
{
if (args[0] == null)
{
throw new IllegalArgumentException("Null MBeanServer");
}
if (_mbs != null)
{
throw new IllegalArgumentException("MBeanServer object already initialized");
}
_mbs = (MBeanServer) args[0];
return null;
}
// Retrieve Subject from current AccessControlContext
AccessControlContext acc = AccessController.getContext();
Subject subject = Subject.getSubject(acc);
try
{
// Allow operations performed locally on behalf of the connector server itself
if (subject == null)
{
return method.invoke(_mbs, args);
}
if (args == null || DELEGATE.equals(args[0]))
{
return method.invoke(_mbs, args);
}
// Restrict access to "createMBean" and "unregisterMBean" to any user
if (methodName.equals("createMBean") || methodName.equals("unregisterMBean"))
{
_logger.debug("User trying to create or unregister an MBean");
throw new SecurityException("Access denied: " + methodName);
}
// Allow querying available object names
if (methodName.equals("queryNames"))
{
return method.invoke(_mbs, args);
}
// Retrieve JMXPrincipal from Subject
Set<JMXPrincipal> principals = subject.getPrincipals(JMXPrincipal.class);
if (principals == null || principals.isEmpty())
{
throw new SecurityException("Access denied: no JMX principal");
}
// Save the subject
SecurityManager.setThreadSubject(subject);
// Get the component, type and impact, which may be null
String type = getType(method, args);
String vhost = getVirtualHost(method, args);
int impact = getImpact(method, args);
// Get the security manager for the virtual host (if set)
SecurityManager security;
if (vhost == null)
{
security = ApplicationRegistry.getInstance().getSecurityManager();
}
else
{
security = ApplicationRegistry.getInstance().getVirtualHostRegistry().getVirtualHost(vhost).getSecurityManager();
}
if (isAccessMethod(methodName) || impact == MBeanOperationInfo.INFO)
{
// Check for read-only method invocation permission
if (!security.authoriseMethod(Operation.ACCESS, type, methodName))
{
throw new SecurityException("Permission denied: Access " + methodName);
}
}
else if (isUpdateMethod(methodName))
{
// Check for setting properties permission
if (!security.authoriseMethod(Operation.UPDATE, type, methodName))
{
throw new SecurityException("Permission denied: Update " + methodName);
}
}
else
{
// Check for invoking/executing method action/operation permission
if (!security.authoriseMethod(Operation.EXECUTE, type, methodName))
{
throw new SecurityException("Permission denied: Execute " + methodName);
}
}
// Actually invoke the method
return method.invoke(_mbs, args);
}
catch (InvocationTargetException e)
{
throw e.getTargetException();
}
}
private String getType(Method method, Object[] args)
{
if (args[0] instanceof ObjectName)
{
ObjectName object = (ObjectName) args[0];
String type = object.getKeyProperty("type");
return type;
}
return null;
}
private String getVirtualHost(Method method, Object[] args)
{
if (args[0] instanceof ObjectName)
{
ObjectName object = (ObjectName) args[0];
String vhost = object.getKeyProperty("VirtualHost");
if(vhost != null)
{
try
{
//if the name is quoted in the ObjectName, unquote it
vhost = ObjectName.unquote(vhost);
}
catch(IllegalArgumentException e)
{
//ignore, this just means the name is not quoted
//and can be left unchanged
}
}
return vhost;
}
return null;
}
private String getMethodName(Method method, Object[] args)
{
String methodName = method.getName();
// if arguments are set, try and work out real method name
if (args != null && args.length >= 1 && args[0] instanceof ObjectName)
{
if (methodName.equals("getAttribute"))
{
methodName = "get" + (String) args[1];
}
else if (methodName.equals("setAttribute"))
{
methodName = "set" + ((Attribute) args[1]).getName();
}
else if (methodName.equals("invoke"))
{
methodName = (String) args[1];
}
}
return methodName;
}
private int getImpact(Method method, Object[] args)
{
//handle invocation of other methods on mbeans
if ((args[0] instanceof ObjectName) && (method.getName().equals("invoke")))
{
//get invoked method name
String mbeanMethod = (args.length > 1) ? (String) args[1] : null;
if (mbeanMethod == null)
{
return -1;
}
try
{
//Get the impact attribute
MBeanInfo mbeanInfo = _mbs.getMBeanInfo((ObjectName) args[0]);
if (mbeanInfo != null)
{
MBeanOperationInfo[] opInfos = mbeanInfo.getOperations();
for (MBeanOperationInfo opInfo : opInfos)
{
if (opInfo.getName().equals(mbeanMethod))
{
return opInfo.getImpact();
}
}
}
}
catch (JMException ex)
{
_logger.error("Unable to determine mbean impact for method : " + mbeanMethod, ex);
}
}
return -1;
}
private boolean isAccessMethod(String methodName)
{
//handle standard get/query/is methods from MBeanServer
return (methodName.startsWith("query") || methodName.startsWith("get") || methodName.startsWith("is"));
}
private boolean isUpdateMethod(String methodName)
{
//handle standard set methods from MBeanServer
return methodName.startsWith("set");
}
public void handleNotification(Notification notification, Object handback)
{
assert notification instanceof JMXConnectionNotification;
// only RMI Connections are serviced here, Local API atta
// rmi://169.24.29.116 guest 3
String[] connectionData = ((JMXConnectionNotification) notification).getConnectionId().split(" ");
String user = connectionData[1];
if (notification.getType().equals(JMXConnectionNotification.OPENED))
{
_logActor.message(ManagementConsoleMessages.OPEN(user));
}
else if (notification.getType().equals(JMXConnectionNotification.CLOSED) ||
notification.getType().equals(JMXConnectionNotification.FAILED))
{
_logActor.message(ManagementConsoleMessages.CLOSE());
}
}
}
| akalankapagoda/andes | modules/andes-core/broker/src/main/java/org/wso2/andes/server/management/MBeanInvocationHandlerImpl.java | Java | apache-2.0 | 11,273 |
package org.batfish.datamodel.vendor_family.juniper;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.annotations.VisibleForTesting;
import java.io.Serializable;
import java.util.Collections;
import java.util.SortedMap;
import java.util.TreeMap;
import org.batfish.datamodel.AaaAuthenticationLoginList;
import org.batfish.datamodel.AuthenticationMethod;
import org.batfish.datamodel.Line;
public class JuniperFamily implements Serializable {
private static final String PROP_LINES = "lines";
private static final String PROP_ROOT_AUTHENTICATION_ENCRYPTED_PASSWORD =
"rootAuthenticationEncryptedPassword";
private static final String PROP_SYSTEM_AUTHENTICATION_ORDER = "systemAuthenticationOrder";
private static final String PROP_TACPLUS_SERVERS = "tacplusServers";
@VisibleForTesting public static final String CONSOLE_LINE_NAME = "console";
@VisibleForTesting public static final String AUXILIARY_LINE_NAME = "auxiliary";
private SortedMap<String, Line> _lines;
private String _rootAuthenticationEncryptedPassword;
private AaaAuthenticationLoginList _systemAuthenticationOrder;
private SortedMap<String, TacplusServer> _tacplusServers;
public JuniperFamily() {
_lines = new TreeMap<>();
_tacplusServers = new TreeMap<>();
_systemAuthenticationOrder = // default authentication order is just password authentication
new AaaAuthenticationLoginList(
Collections.singletonList(AuthenticationMethod.PASSWORD), true);
// Juniper has by default the console and aux lines enabled
Line console = new Line(CONSOLE_LINE_NAME);
console.setAaaAuthenticationLoginList(_systemAuthenticationOrder);
_lines.put(CONSOLE_LINE_NAME, console);
Line aux = new Line(AUXILIARY_LINE_NAME);
aux.setAaaAuthenticationLoginList(_systemAuthenticationOrder);
_lines.put(AUXILIARY_LINE_NAME, aux);
}
@JsonProperty(PROP_LINES)
public SortedMap<String, Line> getLines() {
return _lines;
}
@JsonProperty(PROP_ROOT_AUTHENTICATION_ENCRYPTED_PASSWORD)
public String getRootAuthenticationEncryptedPassword() {
return _rootAuthenticationEncryptedPassword;
}
@JsonProperty(PROP_SYSTEM_AUTHENTICATION_ORDER)
public AaaAuthenticationLoginList getSystemAuthenticationOrder() {
return _systemAuthenticationOrder;
}
@JsonProperty(PROP_TACPLUS_SERVERS)
public SortedMap<String, TacplusServer> getTacplusServers() {
return _tacplusServers;
}
@JsonProperty(PROP_LINES)
public void setLines(SortedMap<String, Line> lines) {
_lines = lines;
}
@JsonProperty(PROP_ROOT_AUTHENTICATION_ENCRYPTED_PASSWORD)
public void setRootAuthenticationEncryptedPassword(String rootAuthenticationEncryptedPassword) {
_rootAuthenticationEncryptedPassword = rootAuthenticationEncryptedPassword;
}
@JsonProperty(PROP_SYSTEM_AUTHENTICATION_ORDER)
public void setSystemAuthenticationOrder(AaaAuthenticationLoginList authenticationOrder) {
_systemAuthenticationOrder = authenticationOrder;
}
@JsonProperty(PROP_TACPLUS_SERVERS)
public void setTacplusServers(SortedMap<String, TacplusServer> tacplusServers) {
_tacplusServers = tacplusServers;
}
}
| batfish/batfish | projects/batfish-common-protocol/src/main/java/org/batfish/datamodel/vendor_family/juniper/JuniperFamily.java | Java | apache-2.0 | 3,192 |
/*
* JBoss, Home of Professional Open Source
* Copyright 2010 Red Hat Inc. and/or its affiliates and other
* contributors as indicated by the @author tags. All rights reserved.
* See the copyright.txt in the distribution for a full listing of
* individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
/**
* Entries which are stored in data containers. This package contains different implementations of
* entries based on the information needed to store an entry. Certain entries need more information - such as timestamps
* and lifespans, if they are used - than others, and the appropriate implementation is selected dynamically. This
* helps minimize Infinispan's memory requirements without storing unnecessary metadata.
*/
package org.infinispan.container.entries; | nmldiegues/stibt | infinispan/core/src/main/java/org/infinispan/container/entries/package-info.java | Java | apache-2.0 | 1,532 |
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.asterix.external.classad;
import java.nio.charset.StandardCharsets;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.asterix.om.base.AMutableInt32;
public class Util {
// convert escapes in-place
// the string can only shrink while converting escapes so we can safely convert in-place.
private static final Pattern OCTAL = Pattern.compile("\\\\([0-3][0-7]{0,2})");
public static boolean convertEscapes(AMutableCharArrayString text) {
boolean validStr = true;
if (text.getLength() == 0) {
return true;
}
int dest = 0;
boolean hasOctal = false;
for (int source = 0; source < text.getLength(); ++source) {
char ch = text.charAt(source);
// scan for escapes, a terminating slash cannot be an escape
if (ch == '\\' && source < text.getLength() - 1) {
++source; // skip the \ character
ch = text.charAt(source);
// The escape part should be re-validated
switch (ch) {
case 'b':
ch = '\b';
break;
case 'f':
ch = '\f';
break;
case 'n':
ch = '\n';
break;
case 'r':
ch = '\r';
break;
case 't':
ch = '\t';
break;
case '\\':
ch = '\\';
break;
default:
if (Lexer.isodigit(ch)) {
hasOctal = true;
++dest;
}
break;
}
}
if (dest == source) {
// no need to assign ch to text when we haven't seen any escapes yet.
// text[dest] = ch;
++dest;
} else {
try {
text.erase(dest);
text.setChar(dest, ch);
++dest;
--source;
} catch (Throwable th) {
th.printStackTrace();
}
}
}
if (dest < text.getLength()) {
text.erase(dest);
text.setLength(dest);
}
// silly, but to fulfull the original contract for this function
// we need to remove the last character in the string if it is a '\0'
// (earlier logic guaranteed that a '\0' can ONLY be the last character)
if (text.getLength() > 0 && (text.charAt(text.getLength() - 1) == '\0')) {
text.erase(text.getLength() - 1);
}
if (hasOctal) {
Matcher m = OCTAL.matcher(text.toString());
StringBuffer out = new StringBuffer();
while (m.find()) {
int octet = Integer.parseInt(m.group(1), 8);
if (octet == 0 || octet > 255) {
return false;
}
m.appendReplacement(out, String.valueOf((char) octet));
}
m.appendTail(out);
text.setValue(new String(out.toString().getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8));
}
return validStr;
}
public static Random initialized = new Random((new Date()).getTime());
public static int getRandomInteger() {
return initialized.nextInt();
}
public static double getRandomReal() {
return initialized.nextDouble();
}
public static int timezoneOffset(ClassAdTime clock) {
return clock.getOffset();
}
public static void getLocalTime(ClassAdTime now, ClassAdTime localtm) {
localtm.setValue(Calendar.getInstance(), now);
localtm.isAbsolute(true);
}
public static void absTimeToString(ClassAdTime atime, AMutableCharArrayString buffer) {
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
//"yyyy-MM-dd'T'HH:mm:ss"
//2004-01-01T00:00:00+11:00
formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
buffer.appendString(formatter.format(atime.getCalendar().getTime()));
buffer.appendString(
(atime.getOffset() >= 0 ? "+" : "-") + String.format("%02d", (Math.abs(atime.getOffset()) / 3600000))
+ ":" + String.format("%02d", ((Math.abs(atime.getOffset() / 60) % 60))));
}
public static void relTimeToString(long rsecs, AMutableCharArrayString buffer) {
double fractional_seconds;
int days, hrs, mins;
double secs;
if (rsecs < 0) {
buffer.appendChar('-');
rsecs = -rsecs;
}
fractional_seconds = rsecs % 1000;
days = (int) (rsecs / 1000);
hrs = days % 86400;
mins = hrs % 3600;
secs = (mins % 60) + (fractional_seconds / 1000.0);
days = days / 86400;
hrs = hrs / 3600;
mins = mins / 60;
if (days != 0) {
if (fractional_seconds == 0) {
buffer.appendString(String.format("%d+%02d:%02d:%02d", days, hrs, mins, (int) secs));
} else {
buffer.appendString(String.format("%d+%02d:%02d:%g", days, hrs, mins, secs));
}
} else if (hrs != 0) {
if (fractional_seconds == 0) {
buffer.appendString(String.format("%02d:%02d:%02d", hrs, mins, (int) secs));
} else {
buffer.appendString(String.format("%02d:%02d:%02g", hrs, mins, secs));
}
} else if (mins != 0) {
if (fractional_seconds == 0) {
buffer.appendString(String.format("%02d:%02d", mins, (int) secs));
} else {
buffer.appendString(String.format("%02d:%02g", mins, secs));
}
return;
} else {
if (fractional_seconds == 0) {
buffer.appendString(String.format("%02d", (int) secs));
} else {
buffer.appendString(String.format("%02g", secs));
}
}
}
public static void dayNumbers(int year, int month, int day, AMutableInt32 weekday, AMutableInt32 yearday) {
int fixed = fixedFromGregorian(year, month, day);
int jan1_fixed = fixedFromGregorian(year, 1, 1);
weekday.setValue(fixed % 7);
yearday.setValue(fixed - jan1_fixed);
return;
}
public static int fixedFromGregorian(int year, int month, int day) {
int fixed;
int month_adjustment;
if (month <= 2) {
month_adjustment = 0;
} else if (isLeapYear(year)) {
month_adjustment = -1;
} else {
month_adjustment = -2;
}
fixed = 365 * (year - 1) + ((year - 1) / 4) - ((year - 1) / 100) + ((year - 1) / 400)
+ ((367 * month - 362) / 12) + month_adjustment + day;
return fixed;
}
public static boolean isLeapYear(int year) {
int mod4;
int mod400;
boolean leap_year;
mod4 = year % 4;
mod400 = year % 400;
if (mod4 == 0 && mod400 != 100 && mod400 != 200 && mod400 != 300) {
leap_year = true;
} else {
leap_year = false;
}
return leap_year;
}
public static int isInf(double x) {
if (Double.isInfinite(x)) {
return (x < 0.0) ? (-1) : 1;
}
return 0;
}
public static boolean isNan(double x) {
return Double.isNaN(x);
}
}
| ty1er/incubator-asterixdb | asterixdb/asterix-external-data/src/test/java/org/apache/asterix/external/classad/Util.java | Java | apache-2.0 | 8,750 |
/*
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.wso2.carbon.identity.entitlement.common.dto;
public class ElementCountDTO {
private int subElementCount;
private int attributeDesignatorsElementCount;
private int attributeValueElementCount;
private int attributeSelectorElementCount;
public int getSubElementCount() {
return subElementCount;
}
public void setSubElementCount(int subElementCount) {
this.subElementCount = subElementCount;
}
public int getAttributeSelectorElementCount() {
return attributeSelectorElementCount;
}
public void setAttributeSelectorElementCount(int attributeSelectorElementCount) {
this.attributeSelectorElementCount = attributeSelectorElementCount;
}
public int getAttributeValueElementCount() {
return attributeValueElementCount;
}
public void setAttributeValueElementCount(int attributeValueElementCount) {
this.attributeValueElementCount = attributeValueElementCount;
}
public int getAttributeDesignatorsElementCount() {
return attributeDesignatorsElementCount;
}
public void setAttributeDesignatorsElementCount(int attributeDesignatorsElementCount) {
this.attributeDesignatorsElementCount = attributeDesignatorsElementCount;
}
}
| dharshanaw/carbon-identity-framework | components/entitlement/org.wso2.carbon.identity.entitlement.common/src/main/java/org/wso2/carbon/identity/entitlement/common/dto/ElementCountDTO.java | Java | apache-2.0 | 1,931 |
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.distributedlog.common.concurrent;
import java.util.LinkedList;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.RejectedExecutionException;
import java.util.function.Supplier;
import javax.annotation.concurrent.GuardedBy;
import org.apache.bookkeeper.common.concurrent.FutureUtils;
import org.apache.distributedlog.common.util.Permit;
/**
* An AsyncSemaphore is a traditional semaphore but with asynchronous
* execution.
*
* <p>Grabbing a permit returns a `Future[Permit]`.
*
* <p>Basic usage:
* {{{
* val semaphore = new AsyncSemaphore(n)
* ...
* semaphore.acquireAndRun() {
* somethingThatReturnsFutureT()
* }
* }}}
*
* <p>Calls to acquire() and acquireAndRun are serialized, and tickets are
* given out fairly (in order of arrival).
*/
public class AsyncSemaphore {
private final Optional<Integer> maxWaiters;
private final Permit semaphorePermit = new Permit() {
@Override
public void release() {
releasePermit(this);
}
};
@GuardedBy("this")
private Optional<Throwable> closed = Optional.empty();
@GuardedBy("this")
private final LinkedList<CompletableFuture<Permit>> waitq;
@GuardedBy("this")
private int availablePermits;
public AsyncSemaphore(int initialPermits,
Optional<Integer> maxWaiters) {
this.availablePermits = initialPermits;
this.waitq = new LinkedList<>();
this.maxWaiters = maxWaiters;
}
private synchronized void releasePermit(Permit permit) {
CompletableFuture<Permit> next = waitq.pollFirst();
if (null != next) {
next.complete(permit);
} else {
availablePermits += 1;
}
}
private CompletableFuture<Permit> newFuturePermit() {
return FutureUtils.value(semaphorePermit);
}
/**
* Acquire a [[Permit]], asynchronously.
*
* <p>Be sure to `permit.release()` in a
* - `finally` block of your `onSuccess` callback
* - `ensure` block of your future chain
*
* <p>Interrupting this future is only advisory, and will not release the permit
* if the future has already been satisfied.
*
* @note This method always return the same instance of [[Permit]].
* @return a `Future[Permit]` when the `Future` is satisfied, computation can proceed,
* or a Future.Exception[RejectedExecutionException]` if the configured maximum
* number of waiters would be exceeded.
*/
public synchronized CompletableFuture<Permit> acquire() {
if (closed.isPresent()) {
return FutureUtils.exception(closed.get());
}
if (availablePermits > 0) {
availablePermits -= 1;
return newFuturePermit();
} else {
if (maxWaiters.isPresent() && waitq.size() >= maxWaiters.get()) {
return FutureUtils.exception(new RejectedExecutionException("Max waiters exceeded"));
} else {
CompletableFuture<Permit> future = FutureUtils.createFuture();
future.whenComplete((value, cause) -> {
synchronized (AsyncSemaphore.this) {
waitq.remove(future);
}
});
waitq.addLast(future);
return future;
}
}
}
/**
* Fail the semaphore and stop it from distributing further permits. Subsequent
* attempts to acquire a permit fail with `exc`. This semaphore's queued waiters
* are also failed with `exc`.
*/
public synchronized void fail(Throwable exc) {
closed = Optional.of(exc);
for (CompletableFuture<Permit> future : waitq) {
future.cancel(true);
}
waitq.clear();
}
/**
* Execute the function asynchronously when a permit becomes available.
*
* <p>If the function throws a non-fatal exception, the exception is returned as part of the Future.
* For all exceptions, the permit would be released before returning.
*
* @return a Future[T] equivalent to the return value of the input function. If the configured
* maximum value of waitq is reached, Future.Exception[RejectedExecutionException] is
* returned.
*/
public <T> CompletableFuture<T> acquireAndRun(Supplier<CompletableFuture<T>> func) {
return acquire().thenCompose(permit -> {
CompletableFuture<T> future;
try {
future = func.get();
future.whenComplete((value, cause) -> permit.release());
return future;
} catch (Throwable cause) {
permit.release();
throw cause;
}
});
}
}
| sijie/bookkeeper | stream/distributedlog/common/src/main/java/org/apache/distributedlog/common/concurrent/AsyncSemaphore.java | Java | apache-2.0 | 5,678 |
package org.asteriskjava.pbx.agi;
import static org.junit.Assert.assertTrue;
import org.asteriskjava.pbx.agi.RateLimiter;
import org.junit.Test;
public class RateLimiterTest
{
@Test
public void test() throws InterruptedException
{
long now = System.currentTimeMillis();
RateLimiter limiter = new RateLimiter(3);
for (int i = 0; i < 15; i++)
{
limiter.acquire();
System.out.println(System.currentTimeMillis());
Thread.sleep(100);
}
// this should have taken around 5 seconds
assertTrue(System.currentTimeMillis() - now > 4000L);
}
}
| pk1057/asterisk-java | src/test/java/org/asteriskjava/pbx/agi/RateLimiterTest.java | Java | apache-2.0 | 647 |
/*
* Copyright 2000-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.openapi.vfs.encoding;
import com.intellij.AppTopics;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.fileEditor.FileDocumentManagerAdapter;
import com.intellij.openapi.fileEditor.impl.LoadTextUtil;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.fileTypes.FileTypes;
import com.intellij.openapi.fileTypes.StdFileTypes;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectLocator;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.ThrowableComputable;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.*;
import com.intellij.refactoring.util.CommonRefactoringUtil;
import com.intellij.util.ArrayUtil;
import com.intellij.util.messages.MessageBusConnection;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Arrays;
public class EncodingUtil {
enum Magic8 {
ABSOLUTELY,
WELL_IF_YOU_INSIST,
NO_WAY
}
// check if file can be loaded in the encoding correctly:
// returns true if bytes on disk, converted to text with the charset, converted back to bytes matched
static Magic8 isSafeToReloadIn(@NotNull VirtualFile virtualFile, @NotNull String text, @NotNull byte[] bytes, @NotNull Charset charset) {
// file has BOM but the charset hasn't
byte[] bom = virtualFile.getBOM();
if (bom != null && !CharsetToolkit.canHaveBom(charset, bom)) return Magic8.NO_WAY;
// the charset has mandatory BOM (e.g. UTF-xx) but the file hasn't or has wrong
byte[] mandatoryBom = CharsetToolkit.getMandatoryBom(charset);
if (mandatoryBom != null && !ArrayUtil.startsWith(bytes, mandatoryBom)) return Magic8.NO_WAY;
String loaded = LoadTextUtil.getTextByBinaryPresentation(bytes, charset).toString();
String separator = FileDocumentManager.getInstance().getLineSeparator(virtualFile, null);
String toSave = StringUtil.convertLineSeparators(loaded, separator);
String failReason = LoadTextUtil.wasCharsetDetectedFromBytes(virtualFile);
if (failReason != null && CharsetToolkit.UTF8_CHARSET.equals(virtualFile.getCharset()) && !CharsetToolkit.UTF8_CHARSET.equals(charset)) {
return Magic8.NO_WAY; // can't reload utf8-autodetected file in another charset
}
byte[] bytesToSave;
try {
bytesToSave = toSave.getBytes(charset);
}
catch (UnsupportedOperationException e) {
return Magic8.NO_WAY;
}
if (bom != null && !ArrayUtil.startsWith(bytesToSave, bom)) {
bytesToSave = ArrayUtil.mergeArrays(bom, bytesToSave); // for 2-byte encodings String.getBytes(Charset) adds BOM automatically
}
return !Arrays.equals(bytesToSave, bytes) ? Magic8.NO_WAY : loaded.equals(text) ? Magic8.ABSOLUTELY : Magic8.WELL_IF_YOU_INSIST;
}
static Magic8 isSafeToConvertTo(@NotNull VirtualFile virtualFile, @NotNull String text, @NotNull byte[] bytesOnDisk, @NotNull Charset charset) {
try {
String lineSeparator = FileDocumentManager.getInstance().getLineSeparator(virtualFile, null);
String textToSave = lineSeparator.equals("\n") ? text : StringUtil.convertLineSeparators(text, lineSeparator);
Pair<Charset, byte[]> chosen = LoadTextUtil.chooseMostlyHarmlessCharset(virtualFile.getCharset(), charset, textToSave);
byte[] saved = chosen.second;
CharSequence textLoadedBack = LoadTextUtil.getTextByBinaryPresentation(saved, charset);
return !text.equals(textLoadedBack.toString()) ? Magic8.NO_WAY : Arrays.equals(saved, bytesOnDisk) ? Magic8.ABSOLUTELY : Magic8.WELL_IF_YOU_INSIST;
}
catch (UnsupportedOperationException e) { // unsupported encoding
return Magic8.NO_WAY;
}
}
static void saveIn(@NotNull final Document document,
final Editor editor,
@NotNull final VirtualFile virtualFile,
@NotNull final Charset charset) {
FileDocumentManager documentManager = FileDocumentManager.getInstance();
documentManager.saveDocument(document);
final Project project = ProjectLocator.getInstance().guessProjectForFile(virtualFile);
boolean writable = project == null ? virtualFile.isWritable() : ReadonlyStatusHandler.ensureFilesWritable(project, virtualFile);
if (!writable) {
CommonRefactoringUtil.showErrorHint(project, editor, "Cannot save the file " + virtualFile.getPresentableUrl(), "Unable to Save", null);
return;
}
// first, save the file in the new charset and then mark the file as having the correct encoding
try {
ApplicationManager.getApplication().runWriteAction(new ThrowableComputable<Object, IOException>() {
@Override
public Object compute() throws IOException {
virtualFile.setCharset(charset);
LoadTextUtil.write(project, virtualFile, virtualFile, document.getText(), document.getModificationStamp());
return null;
}
});
}
catch (IOException io) {
Messages.showErrorDialog(project, io.getMessage(), "Error Writing File");
}
EncodingProjectManagerImpl.suppressReloadDuring(() -> EncodingManager.getInstance().setEncoding(virtualFile, charset));
}
static void reloadIn(@NotNull final VirtualFile virtualFile, @NotNull final Charset charset) {
final FileDocumentManager documentManager = FileDocumentManager.getInstance();
//Project project = ProjectLocator.getInstance().guessProjectForFile(myFile);
//if (documentManager.isFileModified(myFile)) {
// int result = Messages.showDialog(project, "File is modified. Reload file anyway?", "File is Modified", new String[]{"Reload", "Cancel"}, 0, AllIcons.General.WarningDialog);
// if (result != 0) return;
//}
if (documentManager.getCachedDocument(virtualFile) == null) {
// no need to reload document
EncodingManager.getInstance().setEncoding(virtualFile, charset);
return;
}
final Disposable disposable = Disposer.newDisposable();
MessageBusConnection connection = ApplicationManager.getApplication().getMessageBus().connect(disposable);
connection.subscribe(AppTopics.FILE_DOCUMENT_SYNC, new FileDocumentManagerAdapter() {
@Override
public void beforeFileContentReload(VirtualFile file, @NotNull Document document) {
if (!file.equals(virtualFile)) return;
Disposer.dispose(disposable); // disconnect
EncodingManager.getInstance().setEncoding(file, charset);
LoadTextUtil.setCharsetWasDetectedFromBytes(file, null);
}
});
// if file was modified, the user will be asked here
try {
EncodingProjectManagerImpl.suppressReloadDuring(() -> ((VirtualFileListener)documentManager).contentsChanged(
new VirtualFileEvent(null, virtualFile, virtualFile.getName(), virtualFile.getParent())));
}
finally {
Disposer.dispose(disposable);
}
}
// returns (hardcoded charset from the file type, explanation) or (null, null) if file type does not restrict encoding
@NotNull
private static Pair<Charset, String> checkHardcodedCharsetFileType(@NotNull VirtualFile virtualFile) {
FileType fileType = virtualFile.getFileType();
if (fileType.isBinary()) return Pair.create(null, "binary file");
// in lesser IDEs all special file types are plain text so check for that first
if (fileType == FileTypes.PLAIN_TEXT) return Pair.create(null, null);
if (fileType == StdFileTypes.GUI_DESIGNER_FORM) return Pair.create(CharsetToolkit.UTF8_CHARSET, "IDEA GUI Designer form");
if (fileType == StdFileTypes.IDEA_MODULE) return Pair.create(CharsetToolkit.UTF8_CHARSET, "IDEA module file");
if (fileType == StdFileTypes.IDEA_PROJECT) return Pair.create(CharsetToolkit.UTF8_CHARSET, "IDEA project file");
if (fileType == StdFileTypes.IDEA_WORKSPACE) return Pair.create(CharsetToolkit.UTF8_CHARSET, "IDEA workspace file");
if (fileType == StdFileTypes.PROPERTIES) return Pair.create(virtualFile.getCharset(), ".properties file");
if (fileType == StdFileTypes.XML || fileType == StdFileTypes.JSPX) {
return Pair.create(virtualFile.getCharset(), "XML file");
}
return Pair.create(null, null);
}
@NotNull
// returns pair (existing charset (null means N/A); failReason: null means enabled, notnull means disabled and contains error message)
public static Pair<Charset, String> checkCanReload(@NotNull VirtualFile virtualFile) {
if (virtualFile.isDirectory()) {
return Pair.create(null, "file is a directory");
}
FileDocumentManager documentManager = FileDocumentManager.getInstance();
Document document = documentManager.getDocument(virtualFile);
if (document == null) return Pair.create(null, "binary file");
Charset charsetFromContent = ((EncodingManagerImpl)EncodingManager.getInstance()).computeCharsetFromContent(virtualFile);
Charset existing = charsetFromContent;
String failReason = LoadTextUtil.wasCharsetDetectedFromBytes(virtualFile);
if (failReason != null) {
// no point changing encoding if it was auto-detected
existing = virtualFile.getCharset();
}
else if (charsetFromContent != null) {
failReason = "hard coded in text";
}
else {
Pair<Charset, String> fileTypeCheck = checkHardcodedCharsetFileType(virtualFile);
if (fileTypeCheck.second != null) {
failReason = fileTypeCheck.second;
existing = fileTypeCheck.first;
}
}
if (failReason != null) {
return Pair.create(existing, failReason);
}
return Pair.create(virtualFile.getCharset(), null);
}
@Nullable("null means enabled, notnull means disabled and contains error message")
static String checkCanConvert(@NotNull VirtualFile virtualFile) {
if (virtualFile.isDirectory()) {
return "file is a directory";
}
String failReason = null;
Charset charsetFromContent = ((EncodingManagerImpl)EncodingManager.getInstance()).computeCharsetFromContent(virtualFile);
if (charsetFromContent != null) {
failReason = "Encoding is hard-coded in the text";
}
else {
Pair<Charset, String> check = checkHardcodedCharsetFileType(virtualFile);
if (check.second != null) {
failReason = check.second;
}
}
if (failReason != null) {
return failReason;
}
return null;
}
// null means enabled, (current charset, error description) otherwise
@Nullable
public static Pair<Charset, String> checkSomeActionEnabled(@NotNull VirtualFile selectedFile) {
String saveError = checkCanConvert(selectedFile);
if (saveError == null) return null;
Pair<Charset, String> reloadError = checkCanReload(selectedFile);
if (reloadError.second == null) return null;
return Pair.create(reloadError.first, saveError);
}
}
| idea4bsd/idea4bsd | platform/platform-impl/src/com/intellij/openapi/vfs/encoding/EncodingUtil.java | Java | apache-2.0 | 11,775 |
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.opensoc.topology.runner;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Stack;
import oi.thekraken.grok.api.Grok;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.lang.StringUtils;
import org.apache.storm.hdfs.bolt.HdfsBolt;
import org.apache.storm.hdfs.bolt.format.DefaultFileNameFormat;
import org.apache.storm.hdfs.bolt.format.DelimitedRecordFormat;
import org.apache.storm.hdfs.bolt.format.FileNameFormat;
import org.apache.storm.hdfs.bolt.format.RecordFormat;
import org.apache.storm.hdfs.bolt.rotation.FileRotationPolicy;
import org.apache.storm.hdfs.bolt.rotation.FileSizeRotationPolicy;
import org.apache.storm.hdfs.bolt.rotation.FileSizeRotationPolicy.Units;
import org.apache.storm.hdfs.bolt.sync.CountSyncPolicy;
import org.apache.storm.hdfs.bolt.sync.SyncPolicy;
import org.apache.storm.hdfs.common.rotation.MoveFileAction;
import org.json.simple.JSONObject;
import storm.kafka.BrokerHosts;
import storm.kafka.KafkaSpout;
import storm.kafka.SpoutConfig;
import storm.kafka.ZkHosts;
import storm.kafka.bolt.KafkaBolt;
import backtype.storm.Config;
import backtype.storm.LocalCluster;
import backtype.storm.StormSubmitter;
import backtype.storm.generated.Grouping;
import backtype.storm.spout.RawScheme;
import backtype.storm.spout.SchemeAsMultiScheme;
import backtype.storm.topology.BoltDeclarer;
import backtype.storm.topology.TopologyBuilder;
import backtype.storm.tuple.Fields;
import com.esotericsoftware.kryo.serializers.FieldSerializer;
import com.esotericsoftware.kryo.serializers.MapSerializer;
import com.opensoc.alerts.TelemetryAlertsBolt;
import com.opensoc.alerts.adapters.HbaseWhiteAndBlacklistAdapter;
import com.opensoc.alerts.interfaces.AlertsAdapter;
import com.opensoc.enrichment.adapters.cif.CIFHbaseAdapter;
import com.opensoc.enrichment.adapters.geo.GeoMysqlAdapter;
import com.opensoc.enrichment.adapters.host.HostFromPropertiesFileAdapter;
import com.opensoc.enrichment.adapters.whois.WhoisHBaseAdapter;
import com.opensoc.enrichment.adapters.threat.ThreatHbaseAdapter;
import com.opensoc.enrichment.common.GenericEnrichmentBolt;
import com.opensoc.enrichment.interfaces.EnrichmentAdapter;
import com.opensoc.hbase.HBaseBolt;
import com.opensoc.hbase.HBaseStreamPartitioner;
import com.opensoc.hbase.TupleTableConfig;
import com.opensoc.helpers.topology.Cli;
import com.opensoc.helpers.topology.SettingsLoader;
import com.opensoc.index.interfaces.IndexAdapter;
import com.opensoc.indexing.TelemetryIndexingBolt;
import com.opensoc.json.serialization.JSONKryoSerializer;
public abstract class TopologyRunner {
protected Configuration config;
protected TopologyBuilder builder;
protected Config conf;
protected boolean local_mode = true;
protected boolean debug = true;
protected String config_path = null;
protected String default_config_path = "OpenSOC_Configs";
protected boolean success = false;
protected Stack<String> messageComponents = new Stack<String>();
protected Stack<String> errorComponents = new Stack<String>();
protected Stack<String> alertComponents = new Stack<String>();
protected Stack<String> dataComponents = new Stack<String>();
protected Stack<String> terminalComponents = new Stack<String>();
public void initTopology(String args[], String subdir)
throws Exception {
Cli command_line = new Cli(args);
command_line.parse();
System.out.println("[OpenSOC] Starting topology deployment...");
debug = command_line.isDebug();
System.out.println("[OpenSOC] Debug mode set to: " + debug);
local_mode = command_line.isLocal_mode();
System.out.println("[OpenSOC] Local mode set to: " + local_mode);
if (command_line.getPath() != null) {
config_path = command_line.getPath();
System.out
.println("[OpenSOC] Setting config path to external config path: "
+ config_path);
} else {
config_path = default_config_path;
System.out
.println("[OpenSOC] Initializing from default internal config path: "
+ config_path);
}
String topology_conf_path = config_path + "/topologies/" + subdir
+ "/topology.conf";
String environment_identifier_path = config_path
+ "/topologies/environment_identifier.conf";
String topology_identifier_path = config_path + "/topologies/" + subdir
+ "/topology_identifier.conf";
System.out.println("[OpenSOC] Looking for environment identifier: "
+ environment_identifier_path);
System.out.println("[OpenSOC] Looking for topology identifier: "
+ topology_identifier_path);
System.out.println("[OpenSOC] Looking for topology config: "
+ topology_conf_path);
config = new PropertiesConfiguration(topology_conf_path);
JSONObject environment_identifier = SettingsLoader
.loadEnvironmentIdnetifier(environment_identifier_path);
JSONObject topology_identifier = SettingsLoader
.loadTopologyIdnetifier(topology_identifier_path);
String topology_name = SettingsLoader.generateTopologyName(
environment_identifier, topology_identifier);
System.out.println("[OpenSOC] Initializing Topology: " + topology_name);
builder = new TopologyBuilder();
conf = new Config();
conf.registerSerialization(JSONObject.class, MapSerializer.class);
conf.setDebug(debug);
System.out.println("[OpenSOC] Initializing Spout: " + topology_name);
if (command_line.isGenerator_spout()) {
String component_name = config.getString("spout.test.name",
"DefaultTopologySpout");
success = initializeTestingSpout(component_name);
messageComponents.add(component_name);
System.out.println("[OpenSOC] ------Component " + component_name
+ " initialized with the following settings:");
SettingsLoader.printConfigOptions((PropertiesConfiguration) config,
"spout.test");
}
if (!command_line.isGenerator_spout()) {
String component_name = config.getString("spout.kafka.name",
"DefaultTopologyKafkaSpout");
success = initializeKafkaSpout(component_name);
messageComponents.add(component_name);
System.out.println("[OpenSOC] ------Component " + component_name
+ " initialized with the following settings:");
SettingsLoader.printConfigOptions((PropertiesConfiguration) config,
"spout.kafka");
}
if (config.getBoolean("bolt.parser.enabled", true)) {
String component_name = config.getString("bolt.parser.name",
"DefaultTopologyParserBot");
success = initializeParsingBolt(topology_name, component_name);
messageComponents.add(component_name);
errorComponents.add(component_name);
dataComponents.add(component_name);
System.out.println("[OpenSOC] ------Component " + component_name
+ " initialized with the following settings:");
SettingsLoader.printConfigOptions((PropertiesConfiguration) config,
"bolt.parser");
}
if (config.getBoolean("bolt.enrichment.geo.enabled", false)) {
String component_name = config.getString(
"bolt.enrichment.geo.name", "DefaultGeoEnrichmentBolt");
success = initializeGeoEnrichment(topology_name, component_name);
messageComponents.add(component_name);
errorComponents.add(component_name);
System.out.println("[OpenSOC] ------Component " + component_name
+ " initialized with the following settings:");
SettingsLoader.printConfigOptions((PropertiesConfiguration) config,
"bolt.enrichment.geo");
SettingsLoader.printConfigOptions((PropertiesConfiguration) config,
"mysql");
}
if (config.getBoolean("bolt.enrichment.host.enabled", false)) {
String component_name = config.getString(
"bolt.enrichment.host.name", "DefaultHostEnrichmentBolt");
success = initializeHostsEnrichment(topology_name, component_name,
"OpenSOC_Configs/etc/whitelists/known_hosts.conf");
messageComponents.add(component_name);
errorComponents.add(component_name);
System.out.println("[OpenSOC] ------Component " + component_name
+ " initialized with the following settings:");
SettingsLoader.printConfigOptions((PropertiesConfiguration) config,
"bolt.enrichment.host");
}
if (config.getBoolean("bolt.enrichment.whois.enabled", false)) {
String component_name = config.getString(
"bolt.enrichment.whois.name", "DefaultWhoisEnrichmentBolt");
success = initializeWhoisEnrichment(topology_name, component_name);
messageComponents.add(component_name);
errorComponents.add(component_name);
System.out.println("[OpenSOC] ------Component " + component_name
+ " initialized with the following settings:");
SettingsLoader.printConfigOptions((PropertiesConfiguration) config,
"bolt.enrichment.whois");
}
if (config.getBoolean("bolt.enrichment.cif.enabled", false)) {
String component_name = config.getString(
"bolt.enrichment.cif.name", "DefaultCIFEnrichmentBolt");
success = initializeCIFEnrichment(topology_name, component_name);
messageComponents.add(component_name);
errorComponents.add(component_name);
System.out.println("[OpenSOC] ------Component " + component_name
+ " initialized with the following settings:");
SettingsLoader.printConfigOptions((PropertiesConfiguration) config,
"bolt.enrichment.cif");
}
if (config.getBoolean("bolt.enrichment.threat.enabled", false)) {
String component_name = config.getString(
"bolt.enrichment.threat.name", "DefaultThreatEnrichmentBolt");
success = initializeThreatEnrichment(topology_name, component_name);
messageComponents.add(component_name);
errorComponents.add(component_name);
System.out.println("[OpenSOC] ------Component " + component_name
+ " initialized with the following settings:");
SettingsLoader.printConfigOptions((PropertiesConfiguration) config,
"bolt.enrichment.threat");
}
if (config.getBoolean("bolt.alerts.enabled", false)) {
String component_name = config.getString("bolt.alerts.name",
"DefaultAlertsBolt");
success = initializeAlerts(topology_name, component_name,
config_path + "/topologies/" + subdir + "/alerts.xml",
environment_identifier, topology_identifier);
messageComponents.add(component_name);
errorComponents.add(component_name);
alertComponents.add(component_name);
System.out.println("[OpenSOC] ------Component " + component_name
+ " initialized with the following settings:");
SettingsLoader.printConfigOptions((PropertiesConfiguration) config,
"bolt.alerts");
}
if (config.getBoolean("bolt.alerts.indexing.enabled") && config.getBoolean("bolt.alerts.enabled")) {
String component_name = config.getString(
"bolt.alerts.indexing.name", "DefaultAlertsBolt");
success = initializeAlertIndexing(component_name);
terminalComponents.add(component_name);
System.out.println("[OpenSOC] ------Component " + component_name
+ " initialized with the following settings:");
SettingsLoader.printConfigOptions((PropertiesConfiguration) config,
"bolt.alerts.indexing");
}
if (config.getBoolean("bolt.kafka.enabled", false)) {
String component_name = config.getString("bolt.kafka.name",
"DefaultKafkaBolt");
success = initializeKafkaBolt(component_name);
terminalComponents.add(component_name);
System.out.println("[OpenSOC] Component " + component_name
+ " initialized");
System.out.println("[OpenSOC] ------Component " + component_name
+ " initialized with the following settings:");
SettingsLoader.printConfigOptions((PropertiesConfiguration) config,
"bolt.kafka");
}
if (config.getBoolean("bolt.indexing.enabled", true)) {
String component_name = config.getString("bolt.indexing.name",
"DefaultIndexingBolt");
success = initializeIndexingBolt(component_name);
errorComponents.add(component_name);
terminalComponents.add(component_name);
System.out.println("[OpenSOC] ------Component " + component_name
+ " initialized with the following settings:");
SettingsLoader.printConfigOptions((PropertiesConfiguration) config,
"bolt.indexing");
}
if (config.getBoolean("bolt.hdfs.enabled", false)) {
String component_name = config.getString("bolt.hdfs.name",
"DefaultHDFSBolt");
success = initializeHDFSBolt(topology_name, component_name);
terminalComponents.add(component_name);
System.out.println("[OpenSOC] ------Component " + component_name
+ " initialized with the following settings:");
SettingsLoader.printConfigOptions((PropertiesConfiguration) config,
"bolt.hdfs");
}
if (config.getBoolean("bolt.error.indexing.enabled")) {
String component_name = config.getString(
"bolt.error.indexing.name", "DefaultErrorIndexingBolt");
success = initializeErrorIndexBolt(component_name);
terminalComponents.add(component_name);
System.out.println("[OpenSOC] ------Component " + component_name
+ " initialized with the following settings:");
SettingsLoader.printConfigOptions((PropertiesConfiguration) config,
"bolt.error");
}
if (config.containsKey("bolt.hbase.enabled")
&& config.getBoolean("bolt.hbase.enabled")) {
String component_name = config.getString("bolt.hbase.name",
"DefaultHbaseBolt");
String shuffleType = config.getString("bolt.hbase.shuffle.type",
"direct");
success = initializeHbaseBolt(component_name, shuffleType);
terminalComponents.add(component_name);
System.out.println("[OpenSOC] ------Component " + component_name
+ " initialized with the following settings:");
SettingsLoader.printConfigOptions((PropertiesConfiguration) config,
"bolt.hbase");
}
System.out.println("[OpenSOC] Topology Summary: ");
System.out.println("[OpenSOC] Message Stream: "
+ printComponentStream(messageComponents));
System.out.println("[OpenSOC] Alerts Stream: "
+ printComponentStream(alertComponents));
System.out.println("[OpenSOC] Error Stream: "
+ printComponentStream(errorComponents));
System.out.println("[OpenSOC] Data Stream: "
+ printComponentStream(dataComponents));
System.out.println("[OpenSOC] Terminal Components: "
+ printComponentStream(terminalComponents));
if (local_mode) {
conf.setNumWorkers(config.getInt("num.workers"));
conf.setMaxTaskParallelism(1);
LocalCluster cluster = new LocalCluster();
cluster.submitTopology(topology_name, conf,
builder.createTopology());
} else {
conf.setNumWorkers(config.getInt("num.workers"));
conf.setNumAckers(config.getInt("num.ackers"));
StormSubmitter.submitTopology(topology_name, conf,
builder.createTopology());
}
}
private String printComponentStream(List<String> messageComponents) {
StringBuilder print_string = new StringBuilder();
for (String component : messageComponents) {
print_string.append(component + " -> ");
}
print_string.append("[TERMINAL COMPONENT]");
return print_string.toString();
}
public boolean initializeHbaseBolt(String name, String shuffleType) {
try {
String messageUpstreamComponent = dataComponents.get(dataComponents
.size()-1);
System.out.println("[OpenSOC] ------" + name
+ " is initializing from " + messageUpstreamComponent);
String tableName = config.getString("bolt.hbase.table.name")
.toString();
TupleTableConfig hbaseBoltConfig = new TupleTableConfig(tableName,
config.getString("bolt.hbase.table.key.tuple.field.name")
.toString(), config.getString(
"bolt.hbase.table.timestamp.tuple.field.name")
.toString());
String allColumnFamiliesColumnQualifiers = config.getString(
"bolt.hbase.table.fields").toString();
// This is expected in the form
// "<cf1>:<cq11>,<cq12>,<cq13>|<cf2>:<cq21>,<cq22>|......."
String[] tokenizedColumnFamiliesWithColumnQualifiers = StringUtils
.split(allColumnFamiliesColumnQualifiers, "\\|");
for (String tokenizedColumnFamilyWithColumnQualifiers : tokenizedColumnFamiliesWithColumnQualifiers) {
String[] cfCqTokens = StringUtils.split(
tokenizedColumnFamilyWithColumnQualifiers, ":");
String columnFamily = cfCqTokens[0];
String[] columnQualifiers = StringUtils.split(cfCqTokens[1],
",");
for (String columnQualifier : columnQualifiers) {
hbaseBoltConfig.addColumn(columnFamily, columnQualifier);
}
// hbaseBoltConfig.setDurability(Durability.valueOf(conf.get(
// "storm.topology.pcap.bolt.hbase.durability").toString()));
hbaseBoltConfig.setBatch(Boolean.valueOf(config.getString(
"bolt.hbase.enable.batching").toString()));
HBaseBolt hbase_bolt = new HBaseBolt(hbaseBoltConfig,
config.getString("kafka.zk.list"),
config.getString("kafka.zk.port"));
hbase_bolt.setAutoAck(true);
BoltDeclarer declarer = builder.setBolt(name, hbase_bolt,
config.getInt("bolt.hbase.parallelism.hint"))
.setNumTasks(config.getInt("bolt.hbase.num.tasks"));
if (Grouping._Fields.CUSTOM_OBJECT.toString().equalsIgnoreCase(
shuffleType)) {
declarer.customGrouping(
messageUpstreamComponent,
"pcap_data_stream",
new HBaseStreamPartitioner(
hbaseBoltConfig.getTableName(),
0,
Integer.parseInt(conf
.get("bolt.hbase.partitioner.region.info.refresh.interval.mins")
.toString())));
} else if (Grouping._Fields.DIRECT.toString().equalsIgnoreCase(
shuffleType)) {
declarer.fieldsGrouping(messageUpstreamComponent,
"pcap_data_stream", new Fields("pcap_id"));
}
}
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
return true;
}
private boolean initializeErrorIndexBolt(String component_name) {
try {
Class loaded_class = Class.forName(config.getString("bolt.error.indexing.adapter"));
IndexAdapter adapter = (IndexAdapter) loaded_class.newInstance();
String dateFormat = "yyyy.MM";
if (config.containsKey("bolt.alerts.indexing.timestamp")) {
dateFormat = config.getString("bolt.alerts.indexing.timestamp");
}
TelemetryIndexingBolt indexing_bolt = new TelemetryIndexingBolt()
.withIndexIP(config.getString("es.ip"))
.withIndexPort(config.getInt("es.port"))
.withClusterName(config.getString("es.clustername"))
.withIndexName(
config.getString("bolt.error.indexing.indexname"))
.withDocumentName(
config.getString("bolt.error.indexing.documentname"))
.withIndexTimestamp(dateFormat)
.withBulk(config.getInt("bolt.error.indexing.bulk"))
.withIndexAdapter(adapter)
.withMetricConfiguration(config);
BoltDeclarer declarer = builder
.setBolt(
component_name,
indexing_bolt,
config.getInt("bolt.error.indexing.parallelism.hint"))
.setNumTasks(config.getInt("bolt.error.indexing.num.tasks"));
for (String component : errorComponents)
declarer.shuffleGrouping(component, "error");
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
private boolean initializeKafkaSpout(String name) {
try {
BrokerHosts zk = new ZkHosts(config.getString("kafka.zk"));
String input_topic = config.getString("spout.kafka.topic");
SpoutConfig kafkaConfig = new SpoutConfig(zk, input_topic, "",
input_topic);
kafkaConfig.scheme = new SchemeAsMultiScheme(new RawScheme());
kafkaConfig.forceFromStart = Boolean.valueOf("True");
kafkaConfig.startOffsetTime = -1;
builder.setSpout(name, new KafkaSpout(kafkaConfig),
config.getInt("spout.kafka.parallelism.hint")).setNumTasks(
config.getInt("spout.kafka.num.tasks"));
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
return true;
}
abstract boolean initializeParsingBolt(String topology_name, String name);
abstract boolean initializeTestingSpout(String name);
private boolean initializeGeoEnrichment(String topology_name, String name) {
try {
String messageUpstreamComponent = messageComponents
.get(messageComponents.size() - 1);
System.out.println("[OpenSOC] ------" + name
+ " is initializing from " + messageUpstreamComponent);
String[] keys_from_settings = config.getStringArray("bolt.enrichment.geo.fields");
List<String> geo_keys = new ArrayList<String>(Arrays.asList(keys_from_settings));
GeoMysqlAdapter geo_adapter = new GeoMysqlAdapter(
config.getString("mysql.ip"), config.getInt("mysql.port"),
config.getString("mysql.username"),
config.getString("mysql.password"),
config.getString("bolt.enrichment.geo.adapter.table"));
GenericEnrichmentBolt geo_enrichment = new GenericEnrichmentBolt()
.withEnrichmentTag(
config.getString("bolt.enrichment.geo.enrichment_tag"))
.withOutputFieldName(topology_name)
.withAdapter(geo_adapter)
.withMaxTimeRetain(
config.getInt("bolt.enrichment.geo.MAX_TIME_RETAIN_MINUTES"))
.withMaxCacheSize(
config.getInt("bolt.enrichment.geo.MAX_CACHE_SIZE_OBJECTS_NUM"))
.withKeys(geo_keys).withMetricConfiguration(config);
builder.setBolt(name, geo_enrichment,
config.getInt("bolt.enrichment.geo.parallelism.hint"))
.fieldsGrouping(messageUpstreamComponent, "message",
new Fields("key"))
.setNumTasks(config.getInt("bolt.enrichment.geo.num.tasks"));
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
return true;
}
private boolean initializeHostsEnrichment(String topology_name,
String name, String hosts_path) {
try {
String messageUpstreamComponent = messageComponents
.get(messageComponents.size() - 1);
System.out.println("[OpenSOC] ------" + name
+ " is initializing from " + messageUpstreamComponent);
List<String> hosts_keys = new ArrayList<String>();
hosts_keys.add(config.getString("source.ip"));
hosts_keys.add(config.getString("dest.ip"));
Map<String, JSONObject> known_hosts = SettingsLoader
.loadKnownHosts(hosts_path);
HostFromPropertiesFileAdapter host_adapter = new HostFromPropertiesFileAdapter(
known_hosts);
GenericEnrichmentBolt host_enrichment = new GenericEnrichmentBolt()
.withEnrichmentTag(
config.getString("bolt.enrichment.host.enrichment_tag"))
.withAdapter(host_adapter)
.withMaxTimeRetain(
config.getInt("bolt.enrichment.host.MAX_TIME_RETAIN_MINUTES"))
.withMaxCacheSize(
config.getInt("bolt.enrichment.host.MAX_CACHE_SIZE_OBJECTS_NUM"))
.withOutputFieldName(topology_name).withKeys(hosts_keys)
.withMetricConfiguration(config);
builder.setBolt(name, host_enrichment,
config.getInt("bolt.enrichment.host.parallelism.hint"))
.fieldsGrouping(messageUpstreamComponent, "message",
new Fields("key"))
.setNumTasks(
config.getInt("bolt.enrichment.host.num.tasks"));
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
return true;
}
@SuppressWarnings("rawtypes")
private boolean initializeAlerts(String topology_name, String name,
String alerts_path, JSONObject environment_identifier,
JSONObject topology_identifier) {
try {
Class loaded_class = Class.forName(config.getString("bolt.alerts.adapter"));
Constructor constructor = loaded_class.getConstructor(new Class[] { Map.class});
Map<String, String> settings = SettingsLoader.getConfigOptions((PropertiesConfiguration)config, config.getString("bolt.alerts.adapter") + ".");
System.out.println("Adapter Settings: ");
SettingsLoader.printOptionalSettings(settings);
AlertsAdapter alerts_adapter = (AlertsAdapter) constructor.newInstance(settings);
String messageUpstreamComponent = messageComponents
.get(messageComponents.size() - 1);
System.out.println("[OpenSOC] ------" + name
+ " is initializing from " + messageUpstreamComponent);
JSONObject alerts_identifier = SettingsLoader
.generateAlertsIdentifier(environment_identifier,
topology_identifier);
TelemetryAlertsBolt alerts_bolt = new TelemetryAlertsBolt()
.withIdentifier(alerts_identifier).withMaxCacheSize(1000)
.withMaxTimeRetain(3600).withAlertsAdapter(alerts_adapter)
.withOutputFieldName("message")
.withMetricConfiguration(config);
builder.setBolt(name, alerts_bolt,
config.getInt("bolt.alerts.parallelism.hint"))
.fieldsGrouping(messageUpstreamComponent, "message",
new Fields("key"))
.setNumTasks(config.getInt("bolt.alerts.num.tasks"));
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
return true;
}
private boolean initializeAlertIndexing(String name) {
try{
String messageUpstreamComponent = alertComponents.get(alertComponents
.size() - 1);
System.out.println("[OpenSOC] ------" + name + " is initializing from "
+ messageUpstreamComponent);
Class loaded_class = Class.forName(config.getString("bolt.alerts.indexing.adapter"));
IndexAdapter adapter = (IndexAdapter) loaded_class.newInstance();
String dateFormat = "yyyy.MM.dd";
if (config.containsKey("bolt.alerts.indexing.timestamp")) {
dateFormat = config.getString("bolt.alerts.indexing.timestamp");
}
TelemetryIndexingBolt indexing_bolt = new TelemetryIndexingBolt()
.withIndexIP(config.getString("es.ip"))
.withIndexPort(config.getInt("es.port"))
.withClusterName(config.getString("es.clustername"))
.withIndexName(
config.getString("bolt.alerts.indexing.indexname"))
.withDocumentName(
config.getString("bolt.alerts.indexing.documentname"))
.withIndexTimestamp(dateFormat)
.withBulk(config.getInt("bolt.alerts.indexing.bulk"))
.withIndexAdapter(adapter)
.withMetricConfiguration(config);
String alerts_name = config.getString("bolt.alerts.indexing.name");
builder.setBolt(alerts_name, indexing_bolt,
config.getInt("bolt.indexing.parallelism.hint"))
.shuffleGrouping(messageUpstreamComponent, "alert")
.setNumTasks(config.getInt("bolt.indexing.num.tasks"));
}
catch(Exception e)
{
e.printStackTrace();
return false;
}
return true;
}
private boolean initializeKafkaBolt(String name) {
try {
String messageUpstreamComponent = messageComponents
.get(messageComponents.size() - 1);
System.out.println("[OpenSOC] ------" + name
+ " is initializing from " + messageUpstreamComponent);
Map<String, String> kafka_broker_properties = new HashMap<String, String>();
kafka_broker_properties.put("zk.connect",
config.getString("kafka.zk"));
kafka_broker_properties.put("metadata.broker.list",
config.getString("kafka.br"));
kafka_broker_properties.put("serializer.class",
"com.opensoc.json.serialization.JSONKafkaSerializer");
kafka_broker_properties.put("key.serializer.class",
"kafka.serializer.StringEncoder");
String output_topic = config.getString("bolt.kafka.topic");
conf.put("kafka.broker.properties", kafka_broker_properties);
conf.put("topic", output_topic);
builder.setBolt(name, new KafkaBolt<String, JSONObject>(),
config.getInt("bolt.kafka.parallelism.hint"))
.shuffleGrouping(messageUpstreamComponent, "message")
.setNumTasks(config.getInt("bolt.kafka.num.tasks"));
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
return true;
}
private boolean initializeWhoisEnrichment(String topology_name, String name) {
try {
String messageUpstreamComponent = messageComponents
.get(messageComponents.size() - 1);
System.out.println("[OpenSOC] ------" + name
+ " is initializing from " + messageUpstreamComponent);
String[] keys_from_settings = config.getString("bolt.enrichment.whois.fields").split(",");
List<String> whois_keys = new ArrayList<String>(Arrays.asList(keys_from_settings));
EnrichmentAdapter whois_adapter = new WhoisHBaseAdapter(
config.getString("bolt.enrichment.whois.hbase.table.name"),
config.getString("kafka.zk.list"),
config.getString("kafka.zk.port"));
GenericEnrichmentBolt whois_enrichment = new GenericEnrichmentBolt()
.withEnrichmentTag(
config.getString("bolt.enrichment.whois.enrichment_tag"))
.withOutputFieldName(topology_name)
.withAdapter(whois_adapter)
.withMaxTimeRetain(
config.getInt("bolt.enrichment.whois.MAX_TIME_RETAIN_MINUTES"))
.withMaxCacheSize(
config.getInt("bolt.enrichment.whois.MAX_CACHE_SIZE_OBJECTS_NUM"))
.withKeys(whois_keys).withMetricConfiguration(config);
builder.setBolt(name, whois_enrichment,
config.getInt("bolt.enrichment.whois.parallelism.hint"))
.fieldsGrouping(messageUpstreamComponent, "message",
new Fields("key"))
.setNumTasks(
config.getInt("bolt.enrichment.whois.num.tasks"));
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
return true;
}
private boolean initializeIndexingBolt(String name) {
try {
String messageUpstreamComponent = messageComponents
.get(messageComponents.size() - 1);
System.out.println("[OpenSOC] ------" + name
+ " is initializing from " + messageUpstreamComponent);
Class loaded_class = Class.forName(config.getString("bolt.indexing.adapter"));
IndexAdapter adapter = (IndexAdapter) loaded_class.newInstance();
Map<String, String> settings = SettingsLoader.getConfigOptions((PropertiesConfiguration)config, "optional.settings.bolt.index.search.");
if(settings != null && settings.size() > 0)
{
adapter.setOptionalSettings(settings);
System.out.println("[OpenSOC] Index Bolt picket up optional settings:");
SettingsLoader.printOptionalSettings(settings);
}
// dateFormat defaults to hourly if not specified
String dateFormat = "yyyy.MM.dd.hh";
if (config.containsKey("bolt.indexing.timestamp")) {
dateFormat = config.getString("bolt.indexing.timestamp");
}
TelemetryIndexingBolt indexing_bolt = new TelemetryIndexingBolt()
.withIndexIP(config.getString("es.ip"))
.withIndexPort(config.getInt("es.port"))
.withClusterName(config.getString("es.clustername"))
.withIndexName(config.getString("bolt.indexing.indexname"))
.withIndexTimestamp(dateFormat)
.withDocumentName(
config.getString("bolt.indexing.documentname"))
.withBulk(config.getInt("bolt.indexing.bulk"))
.withIndexAdapter(adapter)
.withMetricConfiguration(config);
builder.setBolt(name, indexing_bolt,
config.getInt("bolt.indexing.parallelism.hint"))
.fieldsGrouping(messageUpstreamComponent, "message",
new Fields("key"))
.setNumTasks(config.getInt("bolt.indexing.num.tasks"));
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
return true;
}
private boolean initializeThreatEnrichment(String topology_name, String name) {
try {
String messageUpstreamComponent = messageComponents
.get(messageComponents.size() - 1);
System.out.println("[OpenSOC] ------" + name
+ " is initializing from " + messageUpstreamComponent);
String[] fields = config.getStringArray("bolt.enrichment.threat.fields");
List<String> threat_keys = new ArrayList<String>(Arrays.asList(fields));
GenericEnrichmentBolt threat_enrichment = new GenericEnrichmentBolt()
.withEnrichmentTag(
config.getString("bolt.enrichment.threat.enrichment_tag"))
.withAdapter(
new ThreatHbaseAdapter(config
.getString("kafka.zk.list"), config
.getString("kafka.zk.port"), config
.getString("bolt.enrichment.threat.tablename")))
.withOutputFieldName(topology_name)
.withEnrichmentTag(config.getString("bolt.enrichment.threat.enrichment_tag"))
.withKeys(threat_keys)
.withMaxTimeRetain(
config.getInt("bolt.enrichment.threat.MAX_TIME_RETAIN_MINUTES"))
.withMaxCacheSize(
config.getInt("bolt.enrichment.threat.MAX_CACHE_SIZE_OBJECTS_NUM"))
.withMetricConfiguration(config);
builder.setBolt(name, threat_enrichment,
config.getInt("bolt.enrichment.threat.parallelism.hint"))
.fieldsGrouping(messageUpstreamComponent, "message",
new Fields("key"))
.setNumTasks(config.getInt("bolt.enrichment.threat.num.tasks"));
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
return true;
}
private boolean initializeCIFEnrichment(String topology_name, String name) {
try {
String messageUpstreamComponent = messageComponents
.get(messageComponents.size() - 1);
System.out.println("[OpenSOC] ------" + name
+ " is initializing from " + messageUpstreamComponent);
List<String> cif_keys = new ArrayList<String>();
String[] ipFields = config.getStringArray("bolt.enrichment.cif.fields.ip");
cif_keys.addAll(Arrays.asList(ipFields));
String[] hostFields = config.getStringArray("bolt.enrichment.cif.fields.host");
cif_keys.addAll(Arrays.asList(hostFields));
String[] emailFields = config.getStringArray("bolt.enrichment.cif.fields.email");
cif_keys.addAll(Arrays.asList(emailFields));
GenericEnrichmentBolt cif_enrichment = new GenericEnrichmentBolt()
.withEnrichmentTag(
config.getString("bolt.enrichment.cif.enrichment_tag"))
.withAdapter(
new CIFHbaseAdapter(config
.getString("kafka.zk.list"), config
.getString("kafka.zk.port"), config
.getString("bolt.enrichment.cif.tablename")))
.withOutputFieldName(topology_name)
.withKeys(cif_keys)
.withMaxTimeRetain(
config.getInt("bolt.enrichment.cif.MAX_TIME_RETAIN_MINUTES"))
.withMaxCacheSize(
config.getInt("bolt.enrichment.cif.MAX_CACHE_SIZE_OBJECTS_NUM"))
.withMetricConfiguration(config);
builder.setBolt(name, cif_enrichment,
config.getInt("bolt.enrichment.cif.parallelism.hint"))
.fieldsGrouping(messageUpstreamComponent, "message",
new Fields("key"))
.setNumTasks(config.getInt("bolt.enrichment.cif.num.tasks"));
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
return true;
}
private boolean initializeHDFSBolt(String topology_name, String name) {
try {
String messageUpstreamComponent = messageComponents
.get(messageComponents.size() - 1);
System.out.println("[OpenSOC] ------" + name
+ " is initializing from " + messageUpstreamComponent);
RecordFormat format = new DelimitedRecordFormat()
.withFieldDelimiter(
config.getString("bolt.hdfs.field.delimiter")
.toString()).withFields(
new Fields("message"));
// sync the file system after every x number of tuples
SyncPolicy syncPolicy = new CountSyncPolicy(Integer.valueOf(config
.getString("bolt.hdfs.batch.size").toString()));
// rotate files when they reach certain size
FileRotationPolicy rotationPolicy = new FileSizeRotationPolicy(
Float.valueOf(config.getString(
"bolt.hdfs.file.rotation.size.in.mb").toString()),
Units.MB);
FileNameFormat fileNameFormat = new DefaultFileNameFormat()
.withPath(config.getString("bolt.hdfs.wip.file.path")
.toString());
// Post rotate action
MoveFileAction moveFileAction = (new MoveFileAction())
.toDestination(config.getString(
"bolt.hdfs.finished.file.path").toString());
HdfsBolt hdfsBolt = new HdfsBolt()
.withFsUrl(
config.getString("bolt.hdfs.file.system.url")
.toString())
.withFileNameFormat(fileNameFormat)
.withRecordFormat(format)
.withRotationPolicy(rotationPolicy)
.withSyncPolicy(syncPolicy)
.addRotationAction(moveFileAction);
if (config.getString("bolt.hdfs.compression.codec.class") != null) {
hdfsBolt.withCompressionCodec(config.getString(
"bolt.hdfs.compression.codec.class").toString());
}
builder.setBolt(name, hdfsBolt,
config.getInt("bolt.hdfs.parallelism.hint"))
.shuffleGrouping(messageUpstreamComponent, "message")
.setNumTasks(config.getInt("bolt.hdfs.num.tasks"));
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
return true;
}
}
| OpenSOC/opensoc-streaming | OpenSOC-Topologies/src/main/java/com/opensoc/topology/runner/TopologyRunner.java | Java | apache-2.0 | 36,860 |
/**
* Copyright (C) 2009-2014 Dell, Inc.
* See annotations for authorship information
*
* ====================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*/
package org.dasein.cloud;
/**
* Defines what level data gets cached at.
* <p>Created by AndyLyall: 02/25/14 13:35 PM</p>
* @author Andy Lyall
* @version 2014.03 initial version
* @since 2014.03
*/
public enum VisibleScope {
/**
* Resource is visibile across the entire account
*/
ACCOUNT_GLOBAL,
/**
* Resource is visible across one whole region
*/
ACCOUNT_REGION,
/**
* Resource is visible across one whole datacenter
*/
ACCOUNT_DATACENTER
}
| OSS-TheWeatherCompany/dasein-cloud-core | src/main/java/org/dasein/cloud/VisibleScope.java | Java | apache-2.0 | 1,298 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.