Example usage for java.net URLConnection setDefaultUseCaches

List of usage examples for java.net URLConnection setDefaultUseCaches

Introduction

In this page you can find the example usage for java.net URLConnection setDefaultUseCaches.

Prototype

public void setDefaultUseCaches(boolean defaultusecaches) 

Source Link

Document

Sets the default value of the useCaches field to the specified value.

Usage

From source file:edu.caltechUcla.sselCassel.projects.jMarkets.frontdesk.web.data.SessionBean.java

/**
 * Connect to the JMarkets server and start the session defined by the given SessionDef object.
 *  Return the session Id of the created session
 *//*from   w ww . j  a v  a  2s . c  o m*/
private int executeSession(String path, String name, SessionDef session) {
    try {
        URL servlet = new URL(path + "/servlet/ServletReceiver");
        Request req = new Request(Request.SERVER_INIT_REQUEST);

        req.addIntInfo("numClients", numSubjects);
        req.addIntInfo("updateProtocol", JMConstants.HTTP_UPDATE_PROTOCOL);
        req.addIntInfo("updateTime", 100);
        req.addStringInfo("name", name);
        req.addInfo("session", session);

        URLConnection servletConnection = servlet.openConnection();

        servletConnection.setDoInput(true);
        servletConnection.setDoOutput(true);
        servletConnection.setUseCaches(false);
        servletConnection.setDefaultUseCaches(false);
        servletConnection.setRequestProperty("Content-Type", "application/octet-stream");

        ObjectOutputStream outputToServlet = new ObjectOutputStream(servletConnection.getOutputStream());

        outputToServlet.writeObject(req);
        outputToServlet.flush();
        outputToServlet.close();

        ObjectInputStream inputFromServlet = new ObjectInputStream(servletConnection.getInputStream());
        Response res = (Response) inputFromServlet.readObject();
        int sessionId = res.getIntInfo("sessionId");

        inputFromServlet.close();

        return sessionId;

    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    return -1;
}

From source file:junk.gui.HazardDataSetCalcCondorApp.java

/**
 * this connects to the servlet on web server to check if dataset name already exists
 * or computation have already been for these parameter settings.
 * @return// w w  w. j  a v  a2 s.c  o m
 */
private Object checkForHazardMapComputation() {

    try {
        if (D)
            System.out.println("starting to make connection with servlet");
        URL hazardMapServlet = new URL(DATASET_CHECK_SERVLET_URL);

        URLConnection servletConnection = hazardMapServlet.openConnection();
        if (D)
            System.out.println("connection established");

        // inform the connection that we will send output and accept input
        servletConnection.setDoInput(true);
        servletConnection.setDoOutput(true);

        // Don't use a cached version of URL connection.
        servletConnection.setUseCaches(false);
        servletConnection.setDefaultUseCaches(false);
        // Specify the content type that we will send binary data
        servletConnection.setRequestProperty("Content-Type", "application/octet-stream");

        ObjectOutputStream toServlet = new ObjectOutputStream(servletConnection.getOutputStream());

        //sending the parameters info. to the servlet
        toServlet.writeObject(getParametersInfo());

        //sending the dataset id to the servlet
        toServlet.writeObject(datasetIdText.getText());

        toServlet.flush();
        toServlet.close();

        // Receive the datasetnumber from the servlet after it has received all the data
        ObjectInputStream fromServlet = new ObjectInputStream(servletConnection.getInputStream());
        Object obj = fromServlet.readObject();
        //if(D) System.out.println("Receiving the Input from the Servlet:"+success);
        fromServlet.close();
        return obj;

    } catch (Exception e) {
        ExceptionWindow bugWindow = new ExceptionWindow(this, e, getParametersInfo());
        bugWindow.setVisible(true);
        bugWindow.pack();

    }
    return null;
}

From source file:junk.gui.HazardDataSetCalcCondorApp.java

/**
 * sets up the connection with the servlet on the server (gravity.usc.edu)
 *///ww w .  ja  v a 2 s. co m
private void sendParametersToServlet(SitesInGriddedRegion regionSites,
        ScalarIntensityMeasureRelationshipAPI imr, String eqkRupForecastLocation) {

    try {
        if (D)
            System.out.println("starting to make connection with servlet");
        URL hazardMapServlet = new URL(SERVLET_URL);

        URLConnection servletConnection = hazardMapServlet.openConnection();
        if (D)
            System.out.println("connection established");

        // inform the connection that we will send output and accept input
        servletConnection.setDoInput(true);
        servletConnection.setDoOutput(true);

        // Don't use a cached version of URL connection.
        servletConnection.setUseCaches(false);
        servletConnection.setDefaultUseCaches(false);
        // Specify the content type that we will send binary data
        servletConnection.setRequestProperty("Content-Type", "application/octet-stream");

        ObjectOutputStream toServlet = new ObjectOutputStream(servletConnection.getOutputStream());

        //sending the object of the gridded region sites to the servlet
        toServlet.writeObject(regionSites);
        //sending the IMR object to the servlet
        toServlet.writeObject(imr);
        //sending the EQK forecast object to the servlet
        toServlet.writeObject(eqkRupForecastLocation);
        //send the X values in a arraylist
        ArrayList list = new ArrayList();
        for (int i = 0; i < function.getNum(); ++i)
            list.add(new String("" + function.getX(i)));
        toServlet.writeObject(list);
        // send the MAX DISTANCE
        toServlet.writeObject(maxDistance);

        //sending email address to the servlet
        toServlet.writeObject(emailText.getText());
        //sending the parameters info. to the servlet
        toServlet.writeObject(getParametersInfo());

        //sending the dataset id to the servlet
        toServlet.writeObject(datasetIdText.getText());

        toServlet.flush();
        toServlet.close();

        // Receive the datasetnumber from the servlet after it has received all the data
        ObjectInputStream fromServlet = new ObjectInputStream(servletConnection.getInputStream());
        String dataset = fromServlet.readObject().toString();
        JOptionPane.showMessageDialog(this, dataset);
        if (D)
            System.out.println("Receiving the Input from the Servlet:" + dataset);
        fromServlet.close();

    } catch (Exception e) {
        ExceptionWindow bugWindow = new ExceptionWindow(this, e, getParametersInfo());
        bugWindow.setVisible(true);
        bugWindow.pack();
    }
}

From source file:org.jvnet.hudson.test.JenkinsRule.java

/**
 * Override to tear down your specific external resource.
 *///w ww  .  j  ava 2 s  . co m
public void after() throws Exception {
    try {
        if (jenkins != null) {
            for (EndOfTestListener tl : jenkins.getExtensionList(EndOfTestListener.class))
                tl.onTearDown();
        }

        // cancel pending asynchronous operations, although this doesn't really seem to be working
        for (WebClient client : clients) {
            // unload the page to cancel asynchronous operations
            try {
                client.getPage("about:blank");
            } catch (IOException e) {
                // ignore
            }
            client.closeAllWindows();
        }
        clients.clear();

    } finally {
        try {
            server.stop();
        } catch (Exception e) {
            // ignore
        }
        for (LenientRunnable r : tearDowns)
            try {
                r.run();
            } catch (Exception e) {
                // ignore
            }

        if (jenkins != null)
            jenkins.cleanUp();
        ExtensionList.clearLegacyInstances();
        DescriptorExtensionList.clearLegacyInstances();

        try {
            env.dispose();
        } catch (Exception x) {
            x.printStackTrace();
        }

        if (timeoutTimer != null) {
            timeoutTimer.cancel();
            timeoutTimer = null;
        }

        // Hudson creates ClassLoaders for plugins that hold on to file descriptors of its jar files,
        // but because there's no explicit dispose method on ClassLoader, they won't get GC-ed until
        // at some later point, leading to possible file descriptor overflow. So encourage GC now.
        // see http://bugs.sun.com/view_bug.do?bug_id=4950148
        // TODO use URLClassLoader.close() in Java 7
        System.gc();

        // restore defaultUseCache
        if (Functions.isWindows()) {
            URLConnection aConnection = new File(".").toURI().toURL().openConnection();
            aConnection.setDefaultUseCaches(origDefaultUseCache);
        }
    }
}

From source file:org.jvnet.hudson.test.JenkinsRule.java

/**
 * Override to set up your specific external resource.
 * @throws Throwable if setup fails (which will disable {@code after}
 *///from  www .java2 s .  c  o m
public void before() throws Throwable {
    if (Functions.isWindows()) {
        // JENKINS-4409.
        // URLConnection caches handles to jar files by default,
        // and it prevents delete temporary directories on Windows.
        // Disables caching here.
        // Though defaultUseCache is a static field,
        // its setter and getter are provided as instance methods.
        URLConnection aConnection = new File(".").toURI().toURL().openConnection();
        origDefaultUseCache = aConnection.getDefaultUseCaches();
        aConnection.setDefaultUseCaches(false);
    }

    // Not ideal (https://github.com/junit-team/junit/issues/116) but basically works.
    if (Boolean.getBoolean("ignore.random.failures")) {
        RandomlyFails rf = testDescription.getAnnotation(RandomlyFails.class);
        if (rf != null) {
            throw new AssumptionViolatedException("Known to randomly fail: " + rf.value());
        }
    }

    env = new TestEnvironment(testDescription);
    env.pin();
    recipe();
    AbstractProject.WORKSPACE.toString();
    User.clear();

    try {
        jenkins = hudson = newHudson();
    } catch (Exception e) {
        // if Hudson instance fails to initialize, it leaves the instance field non-empty and break all the rest of the tests, so clean that up.
        Field f = Jenkins.class.getDeclaredField("theInstance");
        f.setAccessible(true);
        f.set(null, null);
        throw e;
    }
    jenkins.setNoUsageStatistics(true); // collecting usage stats from tests are pointless.

    jenkins.setCrumbIssuer(new TestCrumbIssuer());

    jenkins.servletContext.setAttribute("app", jenkins);
    jenkins.servletContext.setAttribute("version", "?");
    WebAppMain.installExpressionFactory(new ServletContextEvent(jenkins.servletContext));

    // set a default JDK to be the one that the harness is using.
    jenkins.getJDKs().add(new JDK("default", System.getProperty("java.home")));

    configureUpdateCenter();

    // expose the test instance as a part of URL tree.
    // this allows tests to use a part of the URL space for itself.
    jenkins.getActions().add(this);

    JenkinsLocationConfiguration.get().setUrl(getURL().toString());

    setUpTimeout();
}

From source file:org.lockss.plugin.PluginManager.java

/**
 * start the plugin manager./*from  w w  w  . j  a  v  a 2  s  .  c  o  m*/
 * @see org.lockss.app.LockssManager#startService()
 */
public void startService() {
    super.startService();
    configMgr = getDaemon().getConfigManager();
    alertMgr = getDaemon().getAlertManager();
    // Initialize the plugin directory.
    initPluginDir();
    configureDefaultTitleSets();
    PluginStatus.register(getDaemon(), this);
    // watch for changes to plugin registry AUs and AUs with stems in hostAus
    registerAuEventHandler(myAuEventHandler);

    if (paramDisableURLConnectionCache) {
        // Up through Java 1.6, even after a ClassLoader is freed, and even
        // if its JarFile is explicitly closed first, the JarFile remains
        // open, using a file descriptor.  This causes open files to
        // accumulate as new versions of plugins are loaded, possibly
        // eventually leading to Too many open files.  Disabling the JarFile
        // cache prevents this from happening.
        try {
            // setDefaultUseCaches() is improperly an instance method, so need to
            // create an instance
            URLConnection foo = new URL("http://example.com/").openConnection();
            log.debug("Disabling URLConnection cache");
            foo.setDefaultUseCaches(false);
        } catch (IOException e) {
            log.warning("Couldn't disable URLConnection cache", e);
        }
    }
    triggerTitleSort();
}