Example usage for java.util.concurrent.atomic AtomicReference set

List of usage examples for java.util.concurrent.atomic AtomicReference set

Introduction

In this page you can find the example usage for java.util.concurrent.atomic AtomicReference set.

Prototype

public final void set(V newValue) 

Source Link

Document

Sets the value to newValue , with memory effects as specified by VarHandle#setVolatile .

Usage

From source file:de.schildbach.pte.AbstractHafasLegacyProvider.java

private QueryTripsResult queryTripsXml(final Context previousContext, final boolean later,
        final CharSequence conReq, final Location from, final @Nullable Location via, final Location to)
        throws IOException {
    final String request = wrapReqC(conReq, null);
    final HttpUrl endpoint = extXmlEndpoint != null ? extXmlEndpoint
            : queryEndpoint.newBuilder().addPathSegment(apiLanguage).build();
    final AtomicReference<QueryTripsResult> result = new AtomicReference<>();
    httpClient.getInputStream(new HttpClient.Callback() {
        @Override/* www  .  j  a  v  a  2  s.  c o  m*/
        public void onSuccessful(final CharSequence bodyPeek, final ResponseBody body) throws IOException {
            try {
                final XmlPullParserFactory factory = XmlPullParserFactory
                        .newInstance(System.getProperty(XmlPullParserFactory.PROPERTY_NAME), null);
                final XmlPullParser pp = factory.newPullParser();
                pp.setInput(body.charStream());

                XmlPullUtil.require(pp, "ResC");
                final String product = XmlPullUtil.attr(pp, "prod").split(" ")[0];
                final ResultHeader header = new ResultHeader(network, SERVER_PRODUCT, product, null, 0, null);
                XmlPullUtil.enter(pp, "ResC");

                if (XmlPullUtil.test(pp, "Err")) {
                    final String code = XmlPullUtil.attr(pp, "code");
                    if (code.equals("I3")) {
                        result.set(new QueryTripsResult(header, QueryTripsResult.Status.INVALID_DATE));
                        return;
                    }
                    if (code.equals("F1")) {
                        result.set(new QueryTripsResult(header, QueryTripsResult.Status.SERVICE_DOWN));
                        return;
                    }
                    throw new IllegalStateException("error " + code + " " + XmlPullUtil.attr(pp, "text"));
                }

                XmlPullUtil.enter(pp, "ConRes");

                if (XmlPullUtil.test(pp, "Err")) {
                    final String code = XmlPullUtil.attr(pp, "code");
                    log.debug("Hafas error: {}", code);
                    if (code.equals("K9260")) {
                        // Unknown departure station
                        result.set(new QueryTripsResult(header, QueryTripsResult.Status.UNKNOWN_FROM));
                        return;
                    }
                    if (code.equals("K9280")) {
                        // Unknown intermediate station
                        result.set(new QueryTripsResult(header, QueryTripsResult.Status.UNKNOWN_VIA));
                        return;
                    }
                    if (code.equals("K9300")) {
                        // Unknown arrival station
                        result.set(new QueryTripsResult(header, QueryTripsResult.Status.UNKNOWN_TO));
                        return;
                    }
                    if (code.equals("K9360")) {
                        // Date outside of the timetable period
                        result.set(new QueryTripsResult(header, QueryTripsResult.Status.INVALID_DATE));
                        return;
                    }
                    if (code.equals("K9380")) {
                        // Dep./Arr./Intermed. or equivalent station defined more than once
                        result.set(new QueryTripsResult(header, QueryTripsResult.Status.TOO_CLOSE));
                        return;
                    }
                    if (code.equals("K895")) {
                        // Departure/Arrival are too near
                        result.set(new QueryTripsResult(header, QueryTripsResult.Status.TOO_CLOSE));
                        return;
                    }
                    if (code.equals("K9220")) {
                        // Nearby to the given address stations could not be found
                        result.set(new QueryTripsResult(header, QueryTripsResult.Status.UNRESOLVABLE_ADDRESS));
                        return;
                    }
                    if (code.equals("K9240")) {
                        // Internal error
                        result.set(new QueryTripsResult(header, QueryTripsResult.Status.SERVICE_DOWN));
                        return;
                    }
                    if (code.equals("K890")) {
                        // No connections found
                        result.set(new QueryTripsResult(header, QueryTripsResult.Status.NO_TRIPS));
                        return;
                    }
                    if (code.equals("K891")) {
                        // No route found (try entering an intermediate station)
                        result.set(new QueryTripsResult(header, QueryTripsResult.Status.NO_TRIPS));
                        return;
                    }
                    if (code.equals("K899")) {
                        // An error occurred
                        result.set(new QueryTripsResult(header, QueryTripsResult.Status.SERVICE_DOWN));
                        return;
                    }
                    if (code.equals("K1:890")) {
                        // Unsuccessful or incomplete search (direction: forward)
                        result.set(new QueryTripsResult(header, QueryTripsResult.Status.NO_TRIPS));
                        return;
                    }
                    if (code.equals("K2:890")) {
                        // Unsuccessful or incomplete search (direction: backward)
                        result.set(new QueryTripsResult(header, QueryTripsResult.Status.NO_TRIPS));
                        return;
                    }
                    throw new IllegalStateException("error " + code + " " + XmlPullUtil.attr(pp, "text"));
                }

                // H9380 Dep./Arr./Intermed. or equivalent stations defined more than once
                // H9360 Error in data field
                // H9320 The input is incorrect or incomplete
                // H9300 Unknown arrival station
                // H9280 Unknown intermediate station
                // H9260 Unknown departure station
                // H9250 Part inquiry interrupted
                // H9240 Unsuccessful search
                // H9230 An internal error occurred
                // H9220 Nearby to the given address stations could not be found
                // H900 Unsuccessful or incomplete search (timetable change)
                // H892 Inquiry too complex (try entering less intermediate stations)
                // H891 No route found (try entering an intermediate station)
                // H890 Unsuccessful search.
                // H500 Because of too many trains the connection is not complete
                // H460 One or more stops are passed through multiple times.
                // H455 Prolonged stop
                // H410 Display may be incomplete due to change of timetable
                // H390 Departure/Arrival replaced by an equivalent station
                // H895 Departure/Arrival are too near
                // H899 Unsuccessful or incomplete search (timetable change

                final String c = XmlPullUtil.optValueTag(pp, "ConResCtxt", null);
                final Context context;
                if (previousContext == null)
                    context = new Context(c, c, 0);
                else if (later)
                    context = new Context(c, previousContext.earlierContext, previousContext.sequence + 1);
                else
                    context = new Context(previousContext.laterContext, c, previousContext.sequence + 1);

                XmlPullUtil.enter(pp, "ConnectionList");

                final List<Trip> trips = new ArrayList<>();

                while (XmlPullUtil.test(pp, "Connection")) {
                    final String id = context.sequence + "/" + XmlPullUtil.attr(pp, "id");

                    XmlPullUtil.enter(pp, "Connection");
                    while (pp.getName().equals("RtStateList"))
                        XmlPullUtil.next(pp);
                    XmlPullUtil.enter(pp, "Overview");

                    final Calendar currentDate = new GregorianCalendar(timeZone);
                    currentDate.clear();
                    parseDate(currentDate, XmlPullUtil.valueTag(pp, "Date"));
                    XmlPullUtil.enter(pp, "Departure");
                    XmlPullUtil.enter(pp, "BasicStop");
                    while (pp.getName().equals("StAttrList"))
                        XmlPullUtil.next(pp);
                    final Location departureLocation = parseLocation(pp);
                    XmlPullUtil.enter(pp, "Dep");
                    XmlPullUtil.skipExit(pp, "Dep");
                    final int[] capacity;
                    if (XmlPullUtil.test(pp, "StopPrognosis")) {
                        XmlPullUtil.enter(pp, "StopPrognosis");
                        XmlPullUtil.optSkip(pp, "Arr");
                        XmlPullUtil.optSkip(pp, "Dep");
                        XmlPullUtil.enter(pp, "Status");
                        XmlPullUtil.skipExit(pp, "Status");
                        final int capacity1st = Integer
                                .parseInt(XmlPullUtil.optValueTag(pp, "Capacity1st", "0"));
                        final int capacity2nd = Integer
                                .parseInt(XmlPullUtil.optValueTag(pp, "Capacity2nd", "0"));
                        if (capacity1st > 0 || capacity2nd > 0)
                            capacity = new int[] { capacity1st, capacity2nd };
                        else
                            capacity = null;
                        XmlPullUtil.skipExit(pp, "StopPrognosis");
                    } else {
                        capacity = null;
                    }
                    XmlPullUtil.skipExit(pp, "BasicStop");
                    XmlPullUtil.skipExit(pp, "Departure");

                    XmlPullUtil.enter(pp, "Arrival");
                    XmlPullUtil.enter(pp, "BasicStop");
                    while (pp.getName().equals("StAttrList"))
                        XmlPullUtil.next(pp);
                    final Location arrivalLocation = parseLocation(pp);
                    XmlPullUtil.skipExit(pp, "BasicStop");
                    XmlPullUtil.skipExit(pp, "Arrival");

                    final int numTransfers = Integer.parseInt(XmlPullUtil.valueTag(pp, "Transfers"));

                    XmlPullUtil.skipExit(pp, "Overview");

                    final List<Trip.Leg> legs = new ArrayList<>(4);

                    XmlPullUtil.enter(pp, "ConSectionList");

                    final Calendar time = new GregorianCalendar(timeZone);

                    while (XmlPullUtil.test(pp, "ConSection")) {
                        XmlPullUtil.enter(pp, "ConSection");

                        // departure
                        XmlPullUtil.enter(pp, "Departure");
                        XmlPullUtil.enter(pp, "BasicStop");
                        while (pp.getName().equals("StAttrList"))
                            XmlPullUtil.next(pp);
                        final Location sectionDepartureLocation = parseLocation(pp);

                        XmlPullUtil.optSkip(pp, "Arr");
                        XmlPullUtil.enter(pp, "Dep");
                        time.setTimeInMillis(currentDate.getTimeInMillis());
                        parseTime(time, XmlPullUtil.valueTag(pp, "Time"));
                        final Date departureTime = time.getTime();
                        final Position departurePos = parsePlatform(pp);
                        XmlPullUtil.skipExit(pp, "Dep");

                        XmlPullUtil.skipExit(pp, "BasicStop");
                        XmlPullUtil.skipExit(pp, "Departure");

                        // journey
                        final Line line;
                        Location destination = null;

                        List<Stop> intermediateStops = null;

                        final String tag = pp.getName();
                        if (tag.equals("Journey")) {
                            XmlPullUtil.enter(pp, "Journey");
                            while (pp.getName().equals("JHandle"))
                                XmlPullUtil.next(pp);
                            XmlPullUtil.enter(pp, "JourneyAttributeList");
                            boolean wheelchairAccess = false;
                            String name = null;
                            String category = null;
                            String shortCategory = null;
                            while (XmlPullUtil.test(pp, "JourneyAttribute")) {
                                XmlPullUtil.enter(pp, "JourneyAttribute");
                                XmlPullUtil.require(pp, "Attribute");
                                final String attrName = XmlPullUtil.attr(pp, "type");
                                final String code = XmlPullUtil.optAttr(pp, "code", null);
                                XmlPullUtil.enter(pp, "Attribute");
                                final Map<String, String> attributeVariants = parseAttributeVariants(pp);
                                XmlPullUtil.skipExit(pp, "Attribute");
                                XmlPullUtil.skipExit(pp, "JourneyAttribute");

                                if ("bf".equals(code)) {
                                    wheelchairAccess = true;
                                } else if ("NAME".equals(attrName)) {
                                    name = attributeVariants.get("NORMAL");
                                } else if ("CATEGORY".equals(attrName)) {
                                    shortCategory = attributeVariants.get("SHORT");
                                    category = attributeVariants.get("NORMAL");
                                    // longCategory = attributeVariants.get("LONG");
                                } else if ("DIRECTION".equals(attrName)) {
                                    final String[] destinationPlaceAndName = splitStationName(
                                            attributeVariants.get("NORMAL"));
                                    destination = new Location(LocationType.ANY, null,
                                            destinationPlaceAndName[0], destinationPlaceAndName[1]);
                                }
                            }
                            XmlPullUtil.skipExit(pp, "JourneyAttributeList");

                            if (XmlPullUtil.test(pp, "PassList")) {
                                intermediateStops = new LinkedList<>();

                                XmlPullUtil.enter(pp, "PassList");
                                while (XmlPullUtil.test(pp, "BasicStop")) {
                                    XmlPullUtil.enter(pp, "BasicStop");
                                    while (XmlPullUtil.test(pp, "StAttrList"))
                                        XmlPullUtil.next(pp);
                                    final Location location = parseLocation(pp);
                                    if (location.id != sectionDepartureLocation.id) {
                                        Date stopArrivalTime = null;
                                        Date stopDepartureTime = null;
                                        Position stopArrivalPosition = null;
                                        Position stopDeparturePosition = null;

                                        if (XmlPullUtil.test(pp, "Arr")) {
                                            XmlPullUtil.enter(pp, "Arr");
                                            time.setTimeInMillis(currentDate.getTimeInMillis());
                                            parseTime(time, XmlPullUtil.valueTag(pp, "Time"));
                                            stopArrivalTime = time.getTime();
                                            stopArrivalPosition = parsePlatform(pp);
                                            XmlPullUtil.skipExit(pp, "Arr");
                                        }

                                        if (XmlPullUtil.test(pp, "Dep")) {
                                            XmlPullUtil.enter(pp, "Dep");
                                            time.setTimeInMillis(currentDate.getTimeInMillis());
                                            parseTime(time, XmlPullUtil.valueTag(pp, "Time"));
                                            stopDepartureTime = time.getTime();
                                            stopDeparturePosition = parsePlatform(pp);
                                            XmlPullUtil.skipExit(pp, "Dep");
                                        }

                                        intermediateStops.add(new Stop(location, stopArrivalTime,
                                                stopArrivalPosition, stopDepartureTime, stopDeparturePosition));
                                    }
                                    XmlPullUtil.skipExit(pp, "BasicStop");
                                }

                                XmlPullUtil.skipExit(pp, "PassList");
                            }

                            XmlPullUtil.skipExit(pp, "Journey");

                            if (category == null)
                                category = shortCategory;

                            line = parseLine(category, name, wheelchairAccess);
                        } else if (tag.equals("Walk") || tag.equals("Transfer") || tag.equals("GisRoute")) {
                            XmlPullUtil.enter(pp);
                            XmlPullUtil.enter(pp, "Duration");
                            XmlPullUtil.skipExit(pp, "Duration");
                            XmlPullUtil.skipExit(pp);

                            line = null;
                        } else {
                            throw new IllegalStateException("cannot handle: " + pp.getName());
                        }

                        // polyline
                        final List<Point> path;
                        if (XmlPullUtil.test(pp, "Polyline")) {
                            path = new LinkedList<>();
                            XmlPullUtil.enter(pp, "Polyline");
                            while (XmlPullUtil.test(pp, "Point")) {
                                final int x = XmlPullUtil.intAttr(pp, "x");
                                final int y = XmlPullUtil.intAttr(pp, "y");
                                path.add(new Point(y, x));
                                XmlPullUtil.next(pp);
                            }
                            XmlPullUtil.skipExit(pp, "Polyline");
                        } else {
                            path = null;
                        }

                        // arrival
                        XmlPullUtil.enter(pp, "Arrival");
                        XmlPullUtil.enter(pp, "BasicStop");
                        while (pp.getName().equals("StAttrList"))
                            XmlPullUtil.next(pp);
                        final Location sectionArrivalLocation = parseLocation(pp);
                        XmlPullUtil.enter(pp, "Arr");
                        time.setTimeInMillis(currentDate.getTimeInMillis());
                        parseTime(time, XmlPullUtil.valueTag(pp, "Time"));
                        final Date arrivalTime = time.getTime();
                        final Position arrivalPos = parsePlatform(pp);
                        XmlPullUtil.skipExit(pp, "Arr");

                        XmlPullUtil.skipExit(pp, "BasicStop");
                        XmlPullUtil.skipExit(pp, "Arrival");

                        // remove last intermediate
                        if (intermediateStops != null)
                            if (!intermediateStops.isEmpty())
                                if (!intermediateStops.get(intermediateStops.size() - 1).location
                                        .equals(sectionArrivalLocation))
                                    intermediateStops.remove(intermediateStops.size() - 1);

                        XmlPullUtil.skipExit(pp, "ConSection");

                        if (line != null) {
                            final Stop departure = new Stop(sectionDepartureLocation, true, departureTime, null,
                                    departurePos, null);
                            final Stop arrival = new Stop(sectionArrivalLocation, false, arrivalTime, null,
                                    arrivalPos, null);

                            legs.add(new Trip.Public(line, destination, departure, arrival, intermediateStops,
                                    path, null));
                        } else {
                            if (legs.size() > 0 && legs.get(legs.size() - 1) instanceof Trip.Individual) {
                                final Trip.Individual lastIndividualLeg = (Trip.Individual) legs
                                        .remove(legs.size() - 1);
                                legs.add(new Trip.Individual(Trip.Individual.Type.WALK,
                                        lastIndividualLeg.departure, lastIndividualLeg.departureTime,
                                        sectionArrivalLocation, arrivalTime, null, 0));
                            } else {
                                legs.add(
                                        new Trip.Individual(Trip.Individual.Type.WALK, sectionDepartureLocation,
                                                departureTime, sectionArrivalLocation, arrivalTime, null, 0));
                            }
                        }
                    }

                    XmlPullUtil.skipExit(pp, "ConSectionList");

                    XmlPullUtil.skipExit(pp, "Connection");

                    trips.add(new Trip(id, departureLocation, arrivalLocation, legs, null, capacity,
                            numTransfers));
                }

                XmlPullUtil.skipExit(pp, "ConnectionList");

                result.set(new QueryTripsResult(header, null, from, via, to, context, trips));
            } catch (final XmlPullParserException x) {
                throw new ParserException("cannot parse xml: " + bodyPeek, x);
            }
        }
    }, endpoint, request, "application/xml", null);

    return result.get();
}

From source file:io.warp10.continuum.egress.EgressFetchHandler.java

private static void rawDump(PrintWriter pw, GTSDecoderIterator iter, boolean dedup, boolean signed,
        long timespan, AtomicReference<Metadata> lastMeta, AtomicLong lastCount, boolean sortMeta)
        throws IOException {

    String name = null;/*from   w  w  w  .  ja v  a 2s  . com*/
    Map<String, String> labels = null;

    StringBuilder sb = new StringBuilder();

    Metadata lastMetadata = lastMeta.get();
    long currentCount = lastCount.get();

    while (iter.hasNext()) {
        GTSDecoder decoder = iter.next();

        if (dedup) {
            decoder = decoder.dedup();
        }

        if (!decoder.next()) {
            continue;
        }

        long toDecodeCount = Long.MAX_VALUE;

        if (timespan < 0) {
            Metadata meta = decoder.getMetadata();
            if (!meta.equals(lastMetadata)) {
                lastMetadata = meta;
                currentCount = 0;
            }
            toDecodeCount = Math.max(0, -timespan - currentCount);
        }

        GTSEncoder encoder = decoder.getEncoder(true);

        //
        // Only display the class + labels if they have changed since the previous GTS
        //

        Map<String, String> lbls = decoder.getLabels();

        //
        // Compute the name
        //

        name = decoder.getName();
        labels = lbls;
        sb.setLength(0);
        GTSHelper.encodeName(sb, name);
        sb.append("{");
        boolean first = true;

        if (sortMeta) {
            lbls = new TreeMap<String, String>(lbls);
        }

        for (Entry<String, String> entry : lbls.entrySet()) {
            //
            // Skip owner/producer labels and any other 'private' labels
            //
            if (!signed) {
                if (Constants.PRODUCER_LABEL.equals(entry.getKey())) {
                    continue;
                }
                if (Constants.OWNER_LABEL.equals(entry.getKey())) {
                    continue;
                }
            }

            if (!first) {
                sb.append(",");
            }
            GTSHelper.encodeName(sb, entry.getKey());
            sb.append("=");
            GTSHelper.encodeName(sb, entry.getValue());
            first = false;
        }
        sb.append("}");

        if (encoder.getCount() > toDecodeCount) {
            // We have too much data, shrink the encoder
            GTSEncoder enc = new GTSEncoder();
            enc.safeSetMetadata(decoder.getMetadata());
            while (decoder.next() && toDecodeCount > 0) {
                enc.addValue(decoder.getTimestamp(), decoder.getLocation(), decoder.getElevation(),
                        decoder.getValue());
                toDecodeCount--;
            }
            encoder = enc;
        }

        if (timespan < 0) {
            currentCount += encoder.getCount();
        }

        if (encoder.size() > 0) {
            pw.print(encoder.getBaseTimestamp());
            pw.print("//");
            pw.print(encoder.getCount());
            pw.print(" ");
            pw.print(sb.toString());
            pw.print(" ");

            //pw.println(new String(OrderPreservingBase64.encode(encoder.getBytes())));
            OrderPreservingBase64.encodeToWriter(encoder.getBytes(), pw);
            pw.write('\r');
            pw.write('\n');
        }
    }

    lastMeta.set(lastMetadata);
    lastCount.set(currentCount);
}

From source file:com.machinepublishers.jbrowserdriver.JBrowserDriver.java

private String launchProcess(final Settings settings, final PortGroup portGroup) {
    final AtomicBoolean ready = new AtomicBoolean();
    final AtomicReference<String> logPrefix = new AtomicReference<String>("");
    new Thread(new Runnable() {
        @Override/*  w  w  w.ja v  a  2  s  . c om*/
        public void run() {
            List<String> myArgs = new ArrayList<String>();
            myArgs.add(settings.javaBinary() == null ? JAVA_BIN : settings.javaBinary());
            myArgs.addAll(inheritedArgs);
            if (!settings.customClasspath()) {
                myArgs.addAll(classpathArgs.get());
            }
            if (settings.javaExportModules()) {
                myArgs.add("-XaddExports:javafx.web/com.sun.webkit.network=ALL-UNNAMED");
                myArgs.add("-XaddExports:javafx.web/com.sun.webkit.network.about=ALL-UNNAMED");
                myArgs.add("-XaddExports:javafx.web/com.sun.webkit.network.data=ALL-UNNAMED");
                myArgs.add("-XaddExports:java.base/sun.net.www.protocol.http=ALL-UNNAMED");
                myArgs.add("-XaddExports:java.base/sun.net.www.protocol.https=ALL-UNNAMED");
                myArgs.add("-XaddExports:java.base/sun.net.www.protocol.file=ALL-UNNAMED");
                myArgs.add("-XaddExports:java.base/sun.net.www.protocol.ftp=ALL-UNNAMED");
                myArgs.add("-XaddExports:java.base/sun.net.www.protocol.jar=ALL-UNNAMED");
                myArgs.add("-XaddExports:java.base/sun.net.www.protocol.mailto=ALL-UNNAMED");
                myArgs.add("-XaddExports:javafx.graphics/com.sun.glass.ui=ALL-UNNAMED");
                myArgs.add("-XaddExports:javafx.web/com.sun.javafx.webkit=ALL-UNNAMED");
                myArgs.add("-XaddExports:javafx.web/com.sun.webkit=ALL-UNNAMED");
            }
            myArgs.add("-Djava.io.tmpdir=" + tmpDir.getAbsolutePath());
            myArgs.add("-Djava.rmi.server.hostname=" + settings.host());
            myArgs.addAll(settings.javaOptions());
            myArgs.add(JBrowserDriverServer.class.getName());
            myArgs.add(Long.toString(portGroup.child));
            myArgs.add(Long.toString(portGroup.parent));
            myArgs.add(Long.toString(portGroup.parentAlt));
            try {
                new ProcessExecutor().addListener(new ProcessListener() {
                    @Override
                    public void afterStart(Process proc, ProcessExecutor executor) {
                        process.set(proc);
                    }
                }).redirectOutput(new LogOutputStream() {
                    boolean done = false;

                    @Override
                    protected void processLine(String line) {
                        if (line != null && !line.isEmpty()) {
                            if (!done) {
                                synchronized (ready) {
                                    if (line.startsWith("ready on ports ")) {
                                        String[] parts = line.substring("ready on ports ".length()).split("/");
                                        actualPortGroup.set(new PortGroup(Integer.parseInt(parts[0]),
                                                Integer.parseInt(parts[1]), Integer.parseInt(parts[2])));
                                        logPrefix.set(new StringBuilder().append("[Instance ")
                                                .append(sessionIdCounter.incrementAndGet()).append("][Port ")
                                                .append(actualPortGroup.get().child).append("]").toString());
                                        ready.set(true);
                                        ready.notifyAll();
                                        done = true;
                                    } else {
                                        log(settings.logger(), logPrefix.get(), line);
                                    }
                                }
                            } else {
                                log(settings.logger(), logPrefix.get(), line);
                            }
                        }
                    }
                }).redirectError(new LogOutputStream() {
                    @Override
                    protected void processLine(String line) {
                        log(settings.logger(), logPrefix.get(), line);
                    }
                }).destroyOnExit().command(myArgs).execute();
            } catch (Throwable t) {
                Util.handleException(t);
            }
            synchronized (ready) {
                ready.set(true);
                ready.notifyAll();
            }
        }
    }).start();
    synchronized (ready) {
        while (!ready.get()) {
            try {
                ready.wait();
                break;
            } catch (InterruptedException e) {
            }
        }
    }
    return logPrefix.get();
}

From source file:org.apache.nifi.processors.hive.ConvertAvroToORC.java

@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
    FlowFile flowFile = session.get();//w ww  .  jav a  2s .c  o m
    if (flowFile == null) {
        return;
    }

    try {
        long startTime = System.currentTimeMillis();
        final long stripeSize = context.getProperty(STRIPE_SIZE).asDataSize(DataUnit.B).longValue();
        final int bufferSize = context.getProperty(BUFFER_SIZE).asDataSize(DataUnit.B).intValue();
        final CompressionKind compressionType = CompressionKind
                .valueOf(context.getProperty(COMPRESSION_TYPE).getValue());
        final AtomicReference<Schema> hiveAvroSchema = new AtomicReference<>(null);
        final AtomicInteger totalRecordCount = new AtomicInteger(0);
        final String fileName = flowFile.getAttribute(CoreAttributes.FILENAME.key());
        flowFile = session.write(flowFile, new StreamCallback() {
            @Override
            public void process(final InputStream rawIn, final OutputStream rawOut) throws IOException {
                try (final InputStream in = new BufferedInputStream(rawIn);
                        final OutputStream out = new BufferedOutputStream(rawOut);
                        final DataFileStream<GenericRecord> reader = new DataFileStream<>(in,
                                new GenericDatumReader<>())) {

                    // Create ORC schema from Avro schema
                    Schema avroSchema = reader.getSchema();
                    TypeDescription orcSchema = OrcUtils.getOrcField(avroSchema);

                    if (orcConfig == null) {
                        orcConfig = new Configuration();
                    }
                    OrcFile.WriterOptions options = OrcFile.writerOptions(orcConfig).setSchema(orcSchema)
                            .stripeSize(stripeSize).bufferSize(bufferSize).compress(compressionType)
                            .version(OrcFile.Version.CURRENT);

                    OrcFlowFileWriter orcWriter = new OrcFlowFileWriter(out, new Path(fileName), options);
                    try {
                        VectorizedRowBatch batch = orcSchema.createRowBatch();
                        int recordCount = 0;
                        int recordsInBatch = 0;
                        GenericRecord currRecord = null;
                        while (reader.hasNext()) {
                            currRecord = reader.next(currRecord);
                            List<Schema.Field> fields = currRecord.getSchema().getFields();
                            if (fields != null) {
                                MutableInt[] vectorOffsets = new MutableInt[fields.size()];
                                for (int i = 0; i < fields.size(); i++) {
                                    vectorOffsets[i] = new MutableInt(0);
                                    Schema.Field field = fields.get(i);
                                    Schema fieldSchema = field.schema();
                                    Object o = currRecord.get(field.name());
                                    try {
                                        OrcUtils.putToRowBatch(batch.cols[i], vectorOffsets[i], recordsInBatch,
                                                fieldSchema, o);
                                    } catch (ArrayIndexOutOfBoundsException aioobe) {
                                        getLogger().error(
                                                "Index out of bounds at record {} for column {}, type {}, and object {}",
                                                new Object[] { recordsInBatch, i,
                                                        fieldSchema.getType().getName(), o.toString() },
                                                aioobe);
                                        throw new IOException(aioobe);
                                    }
                                }
                            }
                            recordCount++;
                            recordsInBatch++;

                            if (recordsInBatch == batch.getMaxSize()) {
                                // add batch and start a new one
                                batch.size = recordsInBatch;
                                orcWriter.addRowBatch(batch);
                                batch = orcSchema.createRowBatch();
                                recordsInBatch = 0;
                            }
                        }

                        // If there are records in the batch, add the batch
                        if (recordsInBatch > 0) {
                            batch.size = recordsInBatch;
                            orcWriter.addRowBatch(batch);
                        }

                        hiveAvroSchema.set(avroSchema);
                        totalRecordCount.set(recordCount);
                    } finally {
                        // finished writing this record, close the writer (which will flush to the flow file)
                        orcWriter.close();
                    }
                }
            }
        });

        final String hiveTableName = context.getProperty(HIVE_TABLE_NAME).isSet()
                ? context.getProperty(HIVE_TABLE_NAME).evaluateAttributeExpressions(flowFile).getValue()
                : OrcUtils.normalizeHiveTableName(hiveAvroSchema.get().getFullName());
        String hiveDDL = OrcUtils.generateHiveDDL(hiveAvroSchema.get(), hiveTableName);
        // Add attributes and transfer to success
        flowFile = session.putAttribute(flowFile, RECORD_COUNT_ATTRIBUTE,
                Integer.toString(totalRecordCount.get()));
        flowFile = session.putAttribute(flowFile, HIVE_DDL_ATTRIBUTE, hiveDDL);
        StringBuilder newFilename = new StringBuilder();
        int extensionIndex = fileName.lastIndexOf(".");
        if (extensionIndex != -1) {
            newFilename.append(fileName.substring(0, extensionIndex));
        } else {
            newFilename.append(fileName);
        }
        newFilename.append(".orc");
        flowFile = session.putAttribute(flowFile, CoreAttributes.MIME_TYPE.key(), ORC_MIME_TYPE);
        flowFile = session.putAttribute(flowFile, CoreAttributes.FILENAME.key(), newFilename.toString());
        session.transfer(flowFile, REL_SUCCESS);
        session.getProvenanceReporter().modifyContent(flowFile,
                "Converted " + totalRecordCount.get() + " records", System.currentTimeMillis() - startTime);
    } catch (final ProcessException pe) {
        getLogger().error("Failed to convert {} from Avro to ORC due to {}; transferring to failure",
                new Object[] { flowFile, pe });
        session.transfer(flowFile, REL_FAILURE);
    }
}

From source file:com.jivesoftware.os.amza.deployable.Main.java

public void run(String[] args) throws Exception {

    String hostname = args[0];//from  w  ww.j av a  2s. co m
    String clusterName = (args.length > 1 ? args[1] : "unnamed");
    String hostPortPeers = (args.length > 2 ? args[2] : null);

    int port = Integer.parseInt(System.getProperty("amza.port", "1175"));
    String multicastGroup = System.getProperty("amza.discovery.group", "225.4.5.6");
    int multicastPort = Integer.parseInt(System.getProperty("amza.discovery.port", "1223"));

    String logicalName = System.getProperty("amza.logicalName", hostname + ":" + port);
    String datacenter = System.getProperty("host.datacenter", "unknownDatacenter");
    String rack = System.getProperty("host.rack", "unknownRack");

    RingMember ringMember = new RingMember(logicalName);
    RingHost ringHost = new RingHost(datacenter, rack, hostname, port);

    // todo need a better way to create writer id.
    int writerId = Integer.parseInt(System.getProperty("amza.id", String.valueOf(new Random().nextInt(512))));

    SnowflakeIdPacker idPacker = new SnowflakeIdPacker();
    JiveEpochTimestampProvider timestampProvider = new JiveEpochTimestampProvider();
    final TimestampedOrderIdProvider orderIdProvider = new OrderIdProviderImpl(
            new ConstantWriterIdProvider(writerId), idPacker, timestampProvider);

    final ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    mapper.configure(SerializationFeature.INDENT_OUTPUT, false);

    final AmzaServiceConfig amzaServiceConfig = new AmzaServiceConfig();
    final AmzaStats amzaSystemStats = new AmzaStats();
    final AmzaStats amzaStats = new AmzaStats();
    final SickThreads sickThreads = new SickThreads();
    final SickPartitions sickPartitions = new SickPartitions();

    AtomicInteger systemRingSize = new AtomicInteger(-1);
    amzaServiceConfig.workingDirectories = System.getProperty("amza.working.dirs", "./data1,./data2,./data3")
            .split(",");
    amzaServiceConfig.systemRingSize = Integer.parseInt(System.getProperty("amza.system.ring.size", "-1"));
    if (amzaServiceConfig.systemRingSize > 0) {
        systemRingSize.set(amzaServiceConfig.systemRingSize);
    }

    AmzaInterner amzaInterner = new AmzaInterner();

    PartitionPropertyMarshaller partitionPropertyMarshaller = new PartitionPropertyMarshaller() {
        @Override
        public PartitionProperties fromBytes(byte[] bytes) {
            try {
                return mapper.readValue(bytes, PartitionProperties.class);
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        }

        @Override
        public byte[] toBytes(PartitionProperties partitionProperties) {
            try {
                return mapper.writeValueAsBytes(partitionProperties);
            } catch (JsonProcessingException ex) {
                throw new RuntimeException(ex);
            }
        }
    };

    //  hmmm
    LABPointerIndexConfig labConfig = BindInterfaceToConfiguration.bindDefault(LABPointerIndexConfig.class);
    labConfig.setLeapCacheMaxCapacity(
            Integer.parseInt(System.getProperty("amza.leap.cache.max.capacity", "1000000")));

    BinaryPrimaryRowMarshaller primaryRowMarshaller = new BinaryPrimaryRowMarshaller(); // hehe you cant change this :)
    BinaryHighwaterRowMarshaller highwaterRowMarshaller = new BinaryHighwaterRowMarshaller(amzaInterner);

    AtomicReference<Callable<RingTopology>> topologyProvider = new AtomicReference<>(); // bit of a hack

    InstanceDescriptor instanceDescriptor = new InstanceDescriptor(datacenter, rack, "", "", "", "", "", "", "",
            "", 0, "", "", "", 0L, true);
    ConnectionDescriptorsProvider connectionsProvider = (connectionDescriptorsRequest,
            expectedReleaseGroup) -> {
        try {
            RingTopology systemRing = topologyProvider.get().call();
            List<ConnectionDescriptor> descriptors = Lists.newArrayList(Iterables.transform(systemRing.entries,
                    input -> new ConnectionDescriptor(instanceDescriptor, false, false,
                            new HostPort(input.ringHost.getHost(), input.ringHost.getPort()),
                            Collections.emptyMap(), Collections.emptyMap())));
            return new ConnectionDescriptorsResponse(200, Collections.emptyList(), "", descriptors,
                    connectionDescriptorsRequest.getRequestUuid());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    };
    TenantsServiceConnectionDescriptorProvider<String> connectionPoolProvider = new TenantsServiceConnectionDescriptorProvider<>(
            Executors.newScheduledThreadPool(1), "", connectionsProvider, "", "", 10_000); // TODO config
    connectionPoolProvider.start();

    TenantAwareHttpClient<String> httpClient = new TenantRoutingHttpClientInitializer<String>(null)
            .builder(connectionPoolProvider, new HttpDeliveryClientHealthProvider("", null, "", 5000, 100))
            .deadAfterNErrors(10).checkDeadEveryNMillis(10_000).maxConnections(1_000)
            .socketTimeoutInMillis(60_000).build(); //TODO expose to conf

    AvailableRowsTaker availableRowsTaker = new HttpAvailableRowsTaker(httpClient, amzaInterner, mapper); // TODO config
    AquariumStats aquariumStats = new AquariumStats();

    AmzaService amzaService = new AmzaServiceInitializer().initialize(amzaServiceConfig, amzaInterner,
            aquariumStats, amzaSystemStats, amzaStats,
            new HealthTimer(CountersAndTimers.getOrCreate("quorumLatency"), "quorumLatency",
                    new NoOpHealthChecker<>("quorumLatency")),
            () -> amzaServiceConfig.systemRingSize, sickThreads, sickPartitions, primaryRowMarshaller,
            highwaterRowMarshaller, ringMember, ringHost, Collections.emptySet(), orderIdProvider, idPacker,
            partitionPropertyMarshaller, (workingIndexDirectories, indexProviderRegistry,
                    ephemeralRowIOProvider, persistentRowIOProvider, partitionStripeFunction) -> {
                indexProviderRegistry
                        .register(
                                new BerkeleyDBWALIndexProvider(BerkeleyDBWALIndexProvider.INDEX_CLASS_NAME,
                                        partitionStripeFunction, workingIndexDirectories),
                                persistentRowIOProvider);

                indexProviderRegistry.register(new LABPointerIndexWALIndexProvider(amzaInterner, labConfig,
                        Executors.newCachedThreadPool(), Executors.newCachedThreadPool(),
                        Executors.newCachedThreadPool(), Executors.newCachedThreadPool(),
                        LABPointerIndexWALIndexProvider.INDEX_CLASS_NAME, partitionStripeFunction,
                        workingIndexDirectories), persistentRowIOProvider);
            }, availableRowsTaker, () -> {
                return new HttpRowsTaker("system", amzaStats, httpClient, mapper, amzaInterner,
                        Executors.newSingleThreadExecutor(), Executors.newCachedThreadPool());
            }, () -> {
                return new HttpRowsTaker("striped", amzaStats, httpClient, mapper, amzaInterner,
                        Executors.newSingleThreadExecutor(), Executors.newCachedThreadPool());
            }, Optional.absent(), (changes) -> {
            }, (threadCount, name) -> {
                return Executors.newCachedThreadPool();
            });

    topologyProvider.set(() -> amzaService.getRingReader().getRing(AmzaRingReader.SYSTEM_RING, -1));

    TailAtScaleStrategy tailAtScaleStrategy = new TailAtScaleStrategy(
            BoundedExecutor.newBoundedExecutor(1024, "tas"), 100, // TODO config
            95, // TODO config
            1000 // TODO config
    );

    AmzaClientProvider<HttpClient, HttpClientException> clientProvider = new AmzaClientProvider<>(
            new HttpPartitionClientFactory(),
            new HttpPartitionHostsProvider(httpClient, tailAtScaleStrategy, mapper),
            new RingHostHttpClientProvider(httpClient), BoundedExecutor.newBoundedExecutor(1024, "amza-client"),
            10_000, //TODO expose to conf
            -1, -1);

    final JerseyEndpoints jerseyEndpoints = new JerseyEndpoints().addEndpoint(AmzaEndpoints.class)
            .addInjectable(AmzaService.class, amzaService).addEndpoint(AmzaReplicationRestEndpoints.class)
            .addInjectable(AmzaInstance.class, amzaService).addEndpoint(AmzaClientRestEndpoints.class)
            .addInjectable(AmzaInterner.class, amzaInterner).addInjectable(ObjectMapper.class, mapper)
            .addInjectable(AmzaClientService.class, new AmzaClientService(amzaService.getRingReader(),
                    amzaService.getRingWriter(), amzaService));

    new AmzaUIInitializer().initialize(clusterName, ringHost, amzaService, clientProvider, aquariumStats,
            amzaStats, timestampProvider, idPacker, amzaInterner, new AmzaUIInitializer.InjectionCallback() {
                @Override
                public void addEndpoint(Class clazz) {
                    System.out.println("Adding endpoint=" + clazz);
                    jerseyEndpoints.addEndpoint(clazz);
                }

                @Override
                public void addInjectable(Class clazz, Object instance) {
                    System.out.println("Injecting " + clazz + " " + instance);
                    jerseyEndpoints.addInjectable(clazz, instance);
                }

                @Override
                public void addSessionAuth(String... paths) throws Exception {
                    System.out.println("Ignoring session auth request for paths: " + Arrays.toString(paths));
                }
            });

    InitializeRestfulServer initializeRestfulServer = new InitializeRestfulServer(false, port, "AmzaNode",
            false, null, null, null, 128, 10000);
    initializeRestfulServer.addClasspathResource("/resources");
    initializeRestfulServer.addContextHandler("/", jerseyEndpoints);
    RestfulServer restfulServer = initializeRestfulServer.build();
    restfulServer.start();

    System.out.println("-----------------------------------------------------------------------");
    System.out.println("|      Jetty Service Online");
    System.out.println("-----------------------------------------------------------------------");

    amzaService.start(ringMember, ringHost);

    System.out.println("-----------------------------------------------------------------------");
    System.out.println("|      Amza Service Online");
    System.out.println("-----------------------------------------------------------------------");

    if (clusterName != null) {
        if (hostPortPeers != null) {
            System.out.println("-----------------------------------------------------------------------");
            System.out.println("|     Amza Service is in manual Discovery mode. Cluster Name:" + clusterName);
            String[] peers = hostPortPeers.split(",");
            for (String peer : peers) {
                String[] hostPort = peer.trim().split(":");
                if (hostPort.length != 2 && hostPort.length != 3) {
                    System.out.println("|     Malformed peer:" + peer
                            + " expected form: <host>:<port> or <logicalName>:<host>:<port>");
                } else {
                    String peerLogicalName = (hostPort.length == 2) ? hostPort[0] + ":" + hostPort[1]
                            : hostPort[0];
                    String peerHostname = (hostPort.length == 2) ? hostPort[0] : hostPort[1];
                    String peerPort = (hostPort.length == 2) ? hostPort[1] : hostPort[2];

                    RingMember peerRingMember = new RingMember(peerLogicalName);
                    RingHost peerRingHost = new RingHost("unknown", "unknown", peerHostname,
                            Integer.parseInt(peerPort));

                    System.out.println("|     Adding ringMember:" + peerRingMember + " on host:" + peerRingHost
                            + " to cluster: " + clusterName);
                    amzaService.getRingWriter().register(peerRingMember, peerRingHost, writerId, false);
                }
            }
            systemRingSize.set(1 + peers.length);
            System.out.println("-----------------------------------------------------------------------");
        } else {
            AmzaDiscovery amzaDiscovery = new AmzaDiscovery(amzaService.getRingReader(),
                    amzaService.getRingWriter(), clusterName, multicastGroup, multicastPort, systemRingSize);
            amzaDiscovery.start();
            System.out.println("-----------------------------------------------------------------------");
            System.out.println("|      Amza Service Discovery Online: Cluster Name:" + clusterName);
            System.out.println("-----------------------------------------------------------------------");
        }
    } else {
        System.out.println("-----------------------------------------------------------------------");
        System.out.println("|     Amza Service is in manual Discovery mode.  No cluster name was specified");
        System.out.println("-----------------------------------------------------------------------");
    }
}

From source file:org.apache.brooklyn.location.jclouds.JcloudsLocation.java

protected LoginCredentials waitForWinRmAvailable(final ComputeService computeService, final NodeMetadata node,
        Optional<HostAndPort> hostAndPortOverride, List<LoginCredentials> credentialsToTry, ConfigBag setup) {
    String waitForWinrmAvailable = setup.get(WAIT_FOR_WINRM_AVAILABLE);
    checkArgument(!"false".equalsIgnoreCase(waitForWinrmAvailable),
            "waitForWinRmAvailable called despite waitForWinRmAvailable=%s", waitForWinrmAvailable);
    Duration timeout = null;//ww  w . ja  v a  2 s  . com
    try {
        timeout = Duration.parse(waitForWinrmAvailable);
    } catch (Exception e) {
        // TODO will this just be a NumberFormatException? If so, catch that specificially
        // normal if 'true'; just fall back to default
        Exceptions.propagateIfFatal(e);
    }
    if (timeout == null) {
        timeout = Duration.parse(WAIT_FOR_WINRM_AVAILABLE.getDefaultValue());
    }

    Set<String> users = Sets.newLinkedHashSet();
    for (LoginCredentials creds : credentialsToTry) {
        users.add(creds.getUser());
    }
    String user = (users.size() == 1) ? Iterables.getOnlyElement(users)
            : "{" + Joiner.on(",").join(users) + "}";
    String vmIp = hostAndPortOverride.isPresent() ? hostAndPortOverride.get().getHostText()
            : getFirstReachableAddress(node, setup);
    if (vmIp == null)
        LOG.warn("Unable to extract IP for " + node + " (" + setup.getDescription()
                + "): subsequent connection attempt will likely fail");
    int vmPort = hostAndPortOverride.isPresent() ? hostAndPortOverride.get().getPortOrDefault(5985) : 5985;

    String connectionDetails = user + "@" + vmIp + ":" + vmPort;
    final HostAndPort hostAndPort = hostAndPortOverride.isPresent() ? hostAndPortOverride.get()
            : HostAndPort.fromParts(vmIp, vmPort);
    final AtomicReference<LoginCredentials> credsSuccessful = new AtomicReference<LoginCredentials>();

    // Don't use config that relates to the final user credentials (those have nothing to do
    // with the initial credentials of the VM returned by the cloud provider).
    // The createTemporaryWinRmMachineLocation deals with removing that.
    ConfigBag winrmProps = ConfigBag.newInstanceCopying(setup);

    final Map<WinRmMachineLocation, LoginCredentials> machinesToTry = Maps.newLinkedHashMap();
    for (LoginCredentials creds : credentialsToTry) {
        machinesToTry.put(createTemporaryWinRmMachineLocation(hostAndPort, creds, winrmProps), creds);
    }
    try {
        Callable<Boolean> checker = new Callable<Boolean>() {
            public Boolean call() {
                for (Map.Entry<WinRmMachineLocation, LoginCredentials> entry : machinesToTry.entrySet()) {
                    WinRmMachineLocation machine = entry.getKey();
                    WinRmToolResponse response = machine.executeCommand(
                            ImmutableMap.of(WinRmTool.PROP_EXEC_TRIES.getName(), 1),
                            ImmutableList.of("echo testing"));
                    boolean success = (response.getStatusCode() == 0);
                    if (success) {
                        credsSuccessful.set(entry.getValue());
                        return true;
                    }
                }
                return false;
            }
        };

        waitForReachable(checker, connectionDetails, credentialsToTry, setup, timeout);
    } finally {
        for (WinRmMachineLocation machine : machinesToTry.keySet()) {
            getManagementContext().getLocationManager().unmanage(machine);
        }
    }

    return credsSuccessful.get();
}

From source file:org.apache.nifi.controller.service.StandardControllerServiceProvider.java

@Override
public ControllerServiceNode createControllerService(final String type, final String id,
        final boolean firstTimeAdded) {
    if (type == null || id == null) {
        throw new NullPointerException();
    }//from w w w.jav  a  2s. co m

    final ClassLoader currentContextClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        final ClassLoader cl = ExtensionManager.getClassLoader(type, id);
        final Class<?> rawClass;

        try {
            if (cl == null) {
                rawClass = Class.forName(type);
            } else {
                Thread.currentThread().setContextClassLoader(cl);
                rawClass = Class.forName(type, false, cl);
            }
        } catch (final Exception e) {
            logger.error("Could not create Controller Service of type " + type + " for ID " + id
                    + "; creating \"Ghost\" implementation", e);
            Thread.currentThread().setContextClassLoader(currentContextClassLoader);
            return createGhostControllerService(type, id);
        }

        final Class<? extends ControllerService> controllerServiceClass = rawClass
                .asSubclass(ControllerService.class);

        final ControllerService originalService = controllerServiceClass.newInstance();
        final AtomicReference<ControllerServiceNode> serviceNodeHolder = new AtomicReference<>(null);
        final InvocationHandler invocationHandler = new InvocationHandler() {
            @Override
            public Object invoke(final Object proxy, final Method method, final Object[] args)
                    throws Throwable {

                final String methodName = method.getName();
                if ("initialize".equals(methodName) || "onPropertyModified".equals(methodName)) {
                    throw new UnsupportedOperationException(
                            method + " may only be invoked by the NiFi framework");
                }

                final ControllerServiceNode node = serviceNodeHolder.get();
                final ControllerServiceState state = node.getState();
                final boolean disabled = state != ControllerServiceState.ENABLED; // only allow method call if service state is ENABLED.
                if (disabled && !validDisabledMethods.contains(method)) {
                    // Use nar class loader here because we are implicitly calling toString() on the original implementation.
                    try (final NarCloseable narCloseable = NarCloseable.withComponentNarLoader(
                            originalService.getClass(), originalService.getIdentifier())) {
                        throw new IllegalStateException("Cannot invoke method " + method
                                + " on Controller Service " + originalService.getIdentifier()
                                + " because the Controller Service is disabled");
                    } catch (final Throwable e) {
                        throw new IllegalStateException(
                                "Cannot invoke method " + method + " on Controller Service with identifier "
                                        + id + " because the Controller Service is disabled");
                    }
                }

                try (final NarCloseable narCloseable = NarCloseable
                        .withComponentNarLoader(originalService.getClass(), originalService.getIdentifier())) {
                    return method.invoke(originalService, args);
                } catch (final InvocationTargetException e) {
                    // If the ControllerService throws an Exception, it'll be wrapped in an InvocationTargetException. We want
                    // to instead re-throw what the ControllerService threw, so we pull it out of the InvocationTargetException.
                    throw e.getCause();
                }
            }
        };

        final ControllerService proxiedService;
        if (cl == null) {
            proxiedService = (ControllerService) Proxy.newProxyInstance(getClass().getClassLoader(),
                    getInterfaces(controllerServiceClass), invocationHandler);
        } else {
            proxiedService = (ControllerService) Proxy.newProxyInstance(cl,
                    getInterfaces(controllerServiceClass), invocationHandler);
        }
        logger.info("Created Controller Service of type {} with identifier {}", type, id);

        final ComponentLog serviceLogger = new SimpleProcessLogger(id, originalService);
        originalService.initialize(new StandardControllerServiceInitializationContext(id, serviceLogger, this,
                getStateManager(id), nifiProperties));

        final ComponentLog logger = new SimpleProcessLogger(id, originalService);
        final ValidationContextFactory validationContextFactory = new StandardValidationContextFactory(this,
                variableRegistry);

        final ControllerServiceNode serviceNode = new StandardControllerServiceNode(proxiedService,
                originalService, id, validationContextFactory, this, variableRegistry, logger);
        serviceNodeHolder.set(serviceNode);
        serviceNode.setName(rawClass.getSimpleName());

        if (firstTimeAdded) {
            try (final NarCloseable x = NarCloseable.withComponentNarLoader(originalService.getClass(),
                    originalService.getIdentifier())) {
                ReflectionUtils.invokeMethodsWithAnnotation(OnAdded.class, originalService);
            } catch (final Exception e) {
                throw new ComponentLifeCycleException(
                        "Failed to invoke On-Added Lifecycle methods of " + originalService, e);
            }
        }

        return serviceNode;
    } catch (final Throwable t) {
        throw new ControllerServiceInstantiationException(t);
    } finally {
        if (currentContextClassLoader != null) {
            Thread.currentThread().setContextClassLoader(currentContextClassLoader);
        }
    }
}

From source file:de.schildbach.pte.AbstractEfaProvider.java

private QueryDeparturesResult xsltDepartureMonitorRequest(final String stationId, final @Nullable Date time,
        final int maxDepartures, final boolean equivs) throws IOException {
    final HttpUrl.Builder url = departureMonitorEndpoint.newBuilder();
    appendXsltDepartureMonitorRequestParameters(url, stationId, time, maxDepartures, equivs);
    final AtomicReference<QueryDeparturesResult> result = new AtomicReference<>();

    final HttpClient.Callback callback = new HttpClient.Callback() {
        @Override//from  w w  w .j a v a 2 s  .  c  om
        public void onSuccessful(final CharSequence bodyPeek, final ResponseBody body) throws IOException {
            try {
                final XmlPullParser pp = parserFactory.newPullParser();
                pp.setInput(body.byteStream(), null); // Read encoding from XML declaration
                final ResultHeader header = enterItdRequest(pp);

                final QueryDeparturesResult r = new QueryDeparturesResult(header);

                XmlPullUtil.enter(pp, "itdDepartureMonitorRequest");
                XmlPullUtil.optSkipMultiple(pp, "itdMessage");

                final String nameState = processItdOdv(pp, "dm", new ProcessItdOdvCallback() {
                    @Override
                    public void location(final String nameState, final Location location,
                            final int matchQuality) {
                        if (location.type == LocationType.STATION)
                            if (findStationDepartures(r.stationDepartures, location.id) == null)
                                r.stationDepartures.add(new StationDepartures(location,
                                        new LinkedList<Departure>(), new LinkedList<LineDestination>()));
                    }
                });

                if ("notidentified".equals(nameState) || "list".equals(nameState)) {
                    result.set(new QueryDeparturesResult(header, QueryDeparturesResult.Status.INVALID_STATION));
                    return;
                }

                XmlPullUtil.optSkip(pp, "itdDateTime");

                XmlPullUtil.optSkip(pp, "itdDMDateTime");

                XmlPullUtil.optSkip(pp, "itdDateRange");

                XmlPullUtil.optSkip(pp, "itdTripOptions");

                XmlPullUtil.optSkip(pp, "itdMessage");

                if (XmlPullUtil.test(pp, "itdServingLines")) {
                    if (!pp.isEmptyElementTag()) {
                        XmlPullUtil.enter(pp, "itdServingLines");
                        while (XmlPullUtil.test(pp, "itdServingLine")) {
                            final String assignedStopId = XmlPullUtil.optAttr(pp, "assignedStopID", null);
                            final LineDestinationAndCancelled lineDestinationAndCancelled = processItdServingLine(
                                    pp);
                            final LineDestination lineDestination = new LineDestination(
                                    lineDestinationAndCancelled.line, lineDestinationAndCancelled.destination);

                            StationDepartures assignedStationDepartures;
                            if (assignedStopId == null)
                                assignedStationDepartures = r.stationDepartures.get(0);
                            else
                                assignedStationDepartures = findStationDepartures(r.stationDepartures,
                                        assignedStopId);

                            if (assignedStationDepartures == null)
                                assignedStationDepartures = new StationDepartures(
                                        new Location(LocationType.STATION, assignedStopId),
                                        new LinkedList<Departure>(), new LinkedList<LineDestination>());

                            final List<LineDestination> assignedStationDeparturesLines = checkNotNull(
                                    assignedStationDepartures.lines);
                            if (!assignedStationDeparturesLines.contains(lineDestination))
                                assignedStationDeparturesLines.add(lineDestination);
                        }
                        XmlPullUtil.skipExit(pp, "itdServingLines");
                    } else {
                        XmlPullUtil.next(pp);
                    }
                } else {
                    result.set(new QueryDeparturesResult(header, QueryDeparturesResult.Status.INVALID_STATION));
                    return;
                }

                XmlPullUtil.require(pp, "itdDepartureList");
                if (XmlPullUtil.optEnter(pp, "itdDepartureList")) {
                    final Calendar plannedDepartureTime = new GregorianCalendar(timeZone);
                    final Calendar predictedDepartureTime = new GregorianCalendar(timeZone);

                    while (XmlPullUtil.test(pp, "itdDeparture")) {
                        final String assignedStopId = XmlPullUtil.attr(pp, "stopID");

                        StationDepartures assignedStationDepartures = findStationDepartures(r.stationDepartures,
                                assignedStopId);
                        if (assignedStationDepartures == null) {
                            final Point coord = processCoordAttr(pp);

                            // final String name = normalizeLocationName(XmlPullUtil.attr(pp, "nameWO"));

                            assignedStationDepartures = new StationDepartures(
                                    new Location(LocationType.STATION, assignedStopId, coord),
                                    new LinkedList<Departure>(), new LinkedList<LineDestination>());
                        }

                        final Position position = parsePosition(XmlPullUtil.optAttr(pp, "platformName", null));

                        XmlPullUtil.enter(pp, "itdDeparture");

                        XmlPullUtil.require(pp, "itdDateTime");
                        plannedDepartureTime.clear();
                        processItdDateTime(pp, plannedDepartureTime);

                        predictedDepartureTime.clear();
                        if (XmlPullUtil.test(pp, "itdRTDateTime"))
                            processItdDateTime(pp, predictedDepartureTime);

                        XmlPullUtil.optSkip(pp, "itdFrequencyInfo");

                        XmlPullUtil.require(pp, "itdServingLine");
                        final boolean isRealtime = XmlPullUtil.attr(pp, "realtime").equals("1");
                        final LineDestinationAndCancelled lineDestinationAndCancelled = processItdServingLine(
                                pp);

                        if (isRealtime && !predictedDepartureTime.isSet(Calendar.HOUR_OF_DAY))
                            predictedDepartureTime.setTimeInMillis(plannedDepartureTime.getTimeInMillis());

                        XmlPullUtil.skipExit(pp, "itdDeparture");

                        if (!lineDestinationAndCancelled.cancelled) {
                            final Departure departure = new Departure(plannedDepartureTime.getTime(),
                                    predictedDepartureTime.isSet(Calendar.HOUR_OF_DAY)
                                            ? predictedDepartureTime.getTime()
                                            : null,
                                    lineDestinationAndCancelled.line, position,
                                    lineDestinationAndCancelled.destination, null, null);
                            assignedStationDepartures.departures.add(departure);
                        }
                    }

                    XmlPullUtil.skipExit(pp, "itdDepartureList");
                }

                result.set(r);
            } catch (final XmlPullParserException x) {
                throw new ParserException("cannot parse xml: " + bodyPeek, x);
            }
        }
    };

    if (httpPost)
        httpClient.getInputStream(callback, url.build(), url.build().encodedQuery(),
                "application/x-www-form-urlencoded", httpReferer);
    else
        httpClient.getInputStream(callback, url.build(), httpReferer);

    return result.get();
}

From source file:org.eclipse.equinox.http.servlet.tests.ServletTest.java

public void basicFilterTest22(String servlet1Pattern, String servlet2Pattern, String filterPattern,
        String expected, String[] dispatchers) throws Exception {
    final AtomicReference<HttpServletRequestWrapper> httpServletRequestWrapper = new AtomicReference<HttpServletRequestWrapper>();
    final AtomicReference<HttpServletResponseWrapper> httpServletResponseWrapper = new AtomicReference<HttpServletResponseWrapper>();

    Servlet servlet1 = new BaseServlet() {
        private static final long serialVersionUID = 1L;

        @Override//  ww w .  j a v a 2 s . co  m
        protected void service(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            request.getRequestDispatcher("index.jsp").forward(request, response);
        }
    };

    Servlet servlet2 = new BaseServlet("a") {
        private static final long serialVersionUID = 1L;

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {

            if ((httpServletRequestWrapper.get() != null) && !request.equals(httpServletRequestWrapper.get())) {
                throw new ServletException("not the same request");
            }
            if ((httpServletResponseWrapper.get() != null)
                    && !response.equals(httpServletResponseWrapper.get())) {
                throw new ServletException("not the same response");
            }

            response.getWriter().print(content);
        }

    };

    Filter filter = new TestFilter() {

        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
                throws IOException, ServletException {

            response.getWriter().write('b');

            httpServletRequestWrapper.set(new HttpServletRequestWrapper((HttpServletRequest) request));
            httpServletResponseWrapper.set(new HttpServletResponseWrapper((HttpServletResponse) response));

            chain.doFilter(httpServletRequestWrapper.get(), httpServletResponseWrapper.get());

            response.getWriter().write('b');
        }

    };

    Dictionary<String, Object> props = new Hashtable<String, Object>();
    props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, servlet1Pattern);
    registrations.add(getBundleContext().registerService(Servlet.class, servlet1, props));

    props = new Hashtable<String, Object>();
    props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, servlet2Pattern);
    registrations.add(getBundleContext().registerService(Servlet.class, servlet2, props));

    props = new Hashtable<String, Object>();
    props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_FILTER_NAME, "F22");
    props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_FILTER_DISPATCHER, dispatchers);
    props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_FILTER_PATTERN, filterPattern);
    registrations.add(getBundleContext().registerService(Filter.class, filter, props));

    String response = requestAdvisor.request("f22/a");

    Assert.assertEquals(expected, response);
}

From source file:de.sainth.recipe.backend.db.repositories.RecipeRepository.java

public Recipe save(Recipe recipe) {
    AtomicReference<Recipe> bu = new AtomicReference<>();
    create.transaction(configuration -> {
        Long id = null;//from w  w w .  j  av a2s . c om
        if (recipe.getId() != null) {
            id = using(configuration).select(RECIPES.ID).from(RECIPES).where(RECIPES.ID.eq(recipe.getId()))
                    .forUpdate().fetchOneInto(Long.class);
        }
        RecipesRecord recipeRecord;
        List<DirectionsRecord> directionsRecords = new ArrayList<>();
        List<IngredientsRecord> ingredientsRecords = new ArrayList<>();
        if (recipe.getId() == null || id == null) {
            recipeRecord = using(configuration)
                    .insertInto(RECIPES, RECIPES.NAME, RECIPES.COMMENT, RECIPES.DIFFICULTY, RECIPES.PORTIONS,
                            RECIPES.AUTHOR, RECIPES.PUBLIC_VISIBLE)
                    .values(recipe.getName(), recipe.getComment(), recipe.getDifficulty(), recipe.getPortions(),
                            recipe.getAuthor().getId(), recipe.isPublicVisible())
                    .returning().fetchOne();
            for (Ingredient ingredient : recipe.getIngredients()) {
                ingredientsRecords.add(using(configuration)
                        .insertInto(INGREDIENTS, INGREDIENTS.AMOUNT, INGREDIENTS.CATEGORY, INGREDIENTS.FOOD,
                                INGREDIENTS.RECIPE)
                        .values(ingredient.getAmount(), ingredient.getCategory(), ingredient.getFood().getId(),
                                recipeRecord.getId())
                        .returning().fetchOne());
            }
            for (Direction direction : recipe.getDirections()) {
                directionsRecords.add(using(configuration)
                        .insertInto(DIRECTIONS, DIRECTIONS.POSITION, DIRECTIONS.DESCRIPTION,
                                DIRECTIONS.DURATION, DIRECTIONS.RECIPE)
                        .values(direction.getPosition(), direction.getDescription(), direction.getDuration(),
                                recipeRecord.getId())
                        .returning().fetchOne());
            }
        } else {
            recipeRecord = using(configuration).update(RECIPES).set(RECIPES.NAME, recipe.getName())
                    .set(RECIPES.COMMENT, recipe.getComment()).set(RECIPES.DIFFICULTY, recipe.getDifficulty())
                    .set(RECIPES.PORTIONS, recipe.getPortions()).set(RECIPES.AUTHOR, recipe.getAuthor().getId())
                    .set(RECIPES.PUBLIC_VISIBLE, recipe.isPublicVisible()).where(RECIPES.ID.eq(id)).returning()
                    .fetchOne();
            using(configuration).delete(INGREDIENTS).where(INGREDIENTS.RECIPE.eq(id));
            for (Ingredient ingredient : recipe.getIngredients()) {
                ingredientsRecords.add(using(configuration)
                        .insertInto(INGREDIENTS, INGREDIENTS.AMOUNT, INGREDIENTS.CATEGORY, INGREDIENTS.FOOD,
                                INGREDIENTS.RECIPE)
                        .values(ingredient.getAmount(), ingredient.getCategory(), ingredient.getFood().getId(),
                                recipeRecord.getId())
                        .returning().fetchOne());
            }
            using(configuration).delete(DIRECTIONS).where(DIRECTIONS.RECIPE.eq(id));
            for (Direction direction : recipe.getDirections()) {
                directionsRecords.add(using(configuration)
                        .insertInto(DIRECTIONS, DIRECTIONS.POSITION, DIRECTIONS.DESCRIPTION,
                                DIRECTIONS.DURATION, DIRECTIONS.RECIPE)
                        .values(direction.getPosition(), direction.getDescription(), direction.getDuration(),
                                recipeRecord.getId())
                        .returning().fetchOne());
            }
        }
        String username = using(configuration).select(USERS.NAME).from(USERS)
                .where(USERS.ID.eq(recipeRecord.getAuthor())).fetchOneInto(String.class);

        List<Ingredient> ingredients = ingredientsRecords.stream().map(
                i -> new Ingredient(i.getAmount(), i.getCategory(), selectFood(configuration, i.getFood())))
                .collect(Collectors.toList());
        List<Direction> directions = directionsRecords.stream()
                .map(d -> new Direction(d.getId(), d.getPosition(), d.getDescription(), d.getDuration()))
                .collect(Collectors.toList());
        bu.set(new Recipe(recipeRecord.getId(), recipeRecord.getName(), recipeRecord.getComment(),
                recipeRecord.getDifficulty(), recipeRecord.getPortions(), recipeRecord.getPublicVisible(),
                new User(recipeRecord.getAuthor(), username), ingredients, directions));
    });

    return bu.get();
}