use of java.util.ConcurrentModificationException in project Mycat-Server by MyCATApache.
the class SelectorUtil method rebuildSelector.
public static Selector rebuildSelector(final Selector oldSelector) throws IOException {
final Selector newSelector;
try {
newSelector = Selector.open();
} catch (Exception e) {
logger.warn("Failed to create a new Selector.", e);
return null;
}
int nChannels = 0;
for (; ; ) {
try {
for (SelectionKey key : oldSelector.keys()) {
Object a = key.attachment();
try {
if (!key.isValid() || key.channel().keyFor(newSelector) != null) {
continue;
}
int interestOps = key.interestOps();
key.cancel();
key.channel().register(newSelector, interestOps, a);
nChannels++;
} catch (Exception e) {
logger.warn("Failed to re-register a Channel to the new Selector.", e);
}
}
} catch (ConcurrentModificationException e) {
// Probably due to concurrent modification of the key set.
continue;
}
break;
}
oldSelector.close();
return newSelector;
}
use of java.util.ConcurrentModificationException in project stanbol by apache.
the class RootResource method performLoadOntology.
protected ResponseBuilder performLoadOntology(MultiPartBody data, HttpHeaders headers, Origin<?>... keys) {
log.debug(" post(MultiPartBody data)");
ResponseBuilder rb = null;
IRI location = null;
// If found, it takes precedence over location.
byte[] file = null;
String format = null;
List<OWLOntologyID> aliases = new ArrayList<OWLOntologyID>();
if (data.getFormFileParameterValues("file").length > 0) {
file = data.getFormFileParameterValues("file")[0].getContent();
}
// else {
if (data.getTextParameterValues("format").length > 0) {
String value = data.getTextParameterValues("format")[0];
if (!value.equals("auto")) {
format = value;
}
}
if (data.getTextParameterValues("url").length > 0) {
String value = data.getTextParameterValues("url")[0];
try {
// To throw 400 if malformed.
URI.create(value);
location = IRI.create(value);
} catch (Exception ex) {
log.error("Malformed IRI for " + value, ex);
throw new WebApplicationException(ex, BAD_REQUEST);
}
}
if (data.getTextParameterValues("alias").length > 0) {
for (String value : data.getTextParameterValues("alias")) {
if (!"null".equals(value)) {
try {
aliases.add(OntologyUtils.decode(value));
} catch (Exception ex) {
log.error("Malformed public key for " + value, ex);
throw new WebApplicationException(ex, BAD_REQUEST);
}
}
}
}
log.debug("Parameters:");
log.debug("file: {}", file != null && file.length > 0 ? "NOT-NULL" : "null");
log.trace("file data: {}", file);
log.debug("url: {}", location);
log.debug("format: {}", format);
log.debug("alias: {}", aliases);
// Then add the file
OWLOntologyID key = null;
if (file != null && file.length > 0) {
/*
* Because the ontology provider's load method could fail after only one attempt without resetting
* the stream, we might have to do that ourselves.
*/
List<String> formats;
if (format != null && !format.trim().isEmpty()) {
formats = Collections.singletonList(format);
} else // The RESTful API has its own list of preferred formats
{
formats = Arrays.asList(RDF_XML, TURTLE, X_TURTLE, N3, N_TRIPLE, OWL_XML, FUNCTIONAL_OWL, MANCHESTER_OWL, RDF_JSON);
}
log.debug("Will try {} supported formats", formats.size());
int unsupported = 0, failed = 0;
Iterator<String> itf = formats.iterator();
if (!itf.hasNext()) {
throw new OntologyLoadingException("No suitable format found or defined.");
}
do {
String f = itf.next();
try {
// Re-instantiate the stream on every attempt
InputStream content = new BufferedInputStream(new ByteArrayInputStream(file));
// ClerezzaOWLUtils.guessOntologyID(new FileInputStream(file), Parser.getInstance(), f);
OWLOntologyID guessed = OWLUtils.guessOntologyID(content, Parser.getInstance(), f);
if (guessed != null && !guessed.isAnonymous() && ontologyProvider.hasOntology(guessed)) {
rb = Response.status(Status.CONFLICT);
this.submitted = guessed;
if (headers.getAcceptableMediaTypes().contains(MediaType.TEXT_HTML_TYPE)) {
rb.entity(new Viewable("/imports/409.ftl", this));
rb.header(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_HTML + "; charset=utf-8");
}
break;
} else {
content = new BufferedInputStream(new ByteArrayInputStream(file));
key = ontologyProvider.loadInStore(content, f, true, keys);
}
} catch (UnsupportedFormatException e) {
log.warn("POST method failed for media type {}. This should not happen (should fail earlier)", headers.getMediaType());
// rb = Response.status(UNSUPPORTED_MEDIA_TYPE);
unsupported++;
} catch (IOException e) {
log.debug(">>> FAILURE format {} (I/O error)", f);
failed++;
} catch (ConcurrentModificationException e) {
log.error("Exception logged", e);
failed++;
} catch (Exception e) {
// SAXParseException and others
log.debug(">>> FAILURE format {} (parse error)", f);
log.debug("Caught exception {} : {}", e.getClass(), e.getLocalizedMessage());
log.trace("Exception logged", e);
failed++;
}
} while ((key == null) && itf.hasNext());
if ((key == null || key.isAnonymous()) && rb == null) {
if (failed > 0) {
throw new WebApplicationException(BAD_REQUEST);
} else if (unsupported > 0) {
throw new WebApplicationException(UNSUPPORTED_MEDIA_TYPE);
}
}
} else if (location != null) {
try {
// Here we try every format supported by the Java API
key = ontologyProvider.loadInStore(location, null, true, keys);
} catch (Exception e) {
log.error("Failed to load ontology from " + location, e);
Throwable cause = e.getCause();
String html = "<h1>400 Bad Request</h1>" + "<p>Failed to load ontology from <a href=\"" + location + "\" target=\"_blank\">" + location + "</a></p>";
if (cause != null)
html += "<p>logged cause was: " + cause.getLocalizedMessage().replace("<", "<").replace(">", ">") + "</p>";
return Response.status(BAD_REQUEST).type(TEXT_HTML).entity(html);
}
} else if (// No content but there are aliases.
!aliases.isEmpty()) {
for (Origin<?> origin : keys) {
if (origin.getReference() instanceof OWLOntologyID) {
OWLOntologyID primary = ((OWLOntologyID) origin.getReference());
if (ontologyProvider.getStatus(primary) != org.apache.stanbol.ontologymanager.servicesapi.ontology.OntologyProvider.Status.NO_MATCH) {
for (OWLOntologyID alias : aliases) {
try {
if (ontologyProvider.addAlias(primary, alias) && key == null) {
key = alias;
}
} catch (IllegalArgumentException ex) {
log.warn("Cannot add alias");
log.warn(" ... ontology key: {}", primary);
log.warn(" ... alias: {}", alias);
log.warn(" ... reason: ", ex);
continue;
}
}
}
}
}
} else {
log.error("Bad request");
log.error(" file is: {}", file);
throw new WebApplicationException(BAD_REQUEST);
}
if (key != null && !key.isAnonymous()) {
String uri = OntologyUtils.encode(key);
if (uri != null && !uri.isEmpty()) {
rb = Response.ok();
if (headers.getAcceptableMediaTypes().contains(MediaType.TEXT_HTML_TYPE)) {
rb.entity(new Viewable("index", this));
rb.header(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_HTML + "; charset=utf-8");
}
} else {
rb = Response.ok();
}
} else if (rb == null) {
rb = Response.status(Status.INTERNAL_SERVER_ERROR);
}
return rb;
}
use of java.util.ConcurrentModificationException in project jena by apache.
the class ArrayBunch method iterator.
@Override
public ExtendedIterator<Triple> iterator(final HashCommon.NotifyEmpty container) {
return new NiceIterator<Triple>() {
protected final int initialChanges = changes;
protected int i = size;
protected final Triple[] e = elements;
@Override
public boolean hasNext() {
if (changes > initialChanges)
throw new ConcurrentModificationException();
return i > 0;
}
@Override
public Triple next() {
if (changes > initialChanges)
throw new ConcurrentModificationException();
if (i == 0)
noElements("no elements left in ArrayBunch iteration");
return e[--i];
}
@Override
public void remove() {
if (changes > initialChanges)
throw new ConcurrentModificationException();
int last = --size;
e[i] = e[last];
e[last] = null;
if (size == 0)
container.emptied();
}
};
}
use of java.util.ConcurrentModificationException in project YCSB by brianfrankcooper.
the class HBaseClient1 method read.
/**
* Read a record from the database. Each field/value pair from the result will
* be stored in a HashMap.
*
* @param table
* The name of the table
* @param key
* The record key of the record to read.
* @param fields
* The list of fields to read, or null for all of them
* @param result
* A HashMap of field/value pairs for the result
* @return Zero on success, a non-zero error code on error
*/
public Status read(String table, String key, Set<String> fields, Map<String, ByteIterator> result) {
// if this is a "new" table, init HTable object. Else, use existing one
if (!tableName.equals(table)) {
currentTable = null;
try {
getHTable(table);
tableName = table;
} catch (IOException e) {
System.err.println("Error accessing HBase table: " + e);
return Status.ERROR;
}
}
Result r = null;
try {
if (debug) {
System.out.println("Doing read from HBase columnfamily " + columnFamily);
System.out.println("Doing read for key: " + key);
}
Get g = new Get(Bytes.toBytes(key));
if (fields == null) {
g.addFamily(columnFamilyBytes);
} else {
for (String field : fields) {
g.addColumn(columnFamilyBytes, Bytes.toBytes(field));
}
}
r = currentTable.get(g);
} catch (IOException e) {
if (debug) {
System.err.println("Error doing get: " + e);
}
return Status.ERROR;
} catch (ConcurrentModificationException e) {
// do nothing for now...need to understand HBase concurrency model better
return Status.ERROR;
}
if (r.isEmpty()) {
return Status.NOT_FOUND;
}
while (r.advance()) {
final Cell c = r.current();
result.put(Bytes.toString(CellUtil.cloneQualifier(c)), new ByteArrayByteIterator(CellUtil.cloneValue(c)));
if (debug) {
System.out.println("Result for field: " + Bytes.toString(CellUtil.cloneQualifier(c)) + " is: " + Bytes.toString(CellUtil.cloneValue(c)));
}
}
return Status.OK;
}
use of java.util.ConcurrentModificationException in project mobile-android by photo.
the class ArrayDeque method delete.
/**
* Removes the element at the specified position in the elements array,
* adjusting head and tail as necessary. This can result in motion of
* elements backwards or forwards in the array.
* <p>
* This method is called delete rather than remove to emphasize that its
* semantics differ from those of {@link List#remove(int)}.
*
* @return true if elements moved backwards
*/
private boolean delete(int i) {
checkInvariants();
final E[] elements = this.elements;
final int mask = elements.length - 1;
final int h = head;
final int t = tail;
final int front = (i - h) & mask;
final int back = (t - i) & mask;
// Invariant: head <= i < tail mod circularity
if (front >= ((t - h) & mask))
throw new ConcurrentModificationException();
// Optimize for least element motion
if (front < back) {
if (h <= i) {
System.arraycopy(elements, h, elements, h + 1, front);
} else {
// Wrap around
System.arraycopy(elements, 0, elements, 1, i);
elements[0] = elements[mask];
System.arraycopy(elements, h, elements, h + 1, mask - h);
}
elements[h] = null;
head = (h + 1) & mask;
return false;
} else {
if (i < t) {
// Copy the null tail as well
System.arraycopy(elements, i + 1, elements, i, back);
tail = t - 1;
} else {
// Wrap around
System.arraycopy(elements, i + 1, elements, i, mask - i);
elements[mask] = elements[0];
System.arraycopy(elements, 1, elements, 0, t);
tail = (t - 1) & mask;
}
return true;
}
}
Aggregations