Example usage for java.lang.ref WeakReference WeakReference

List of usage examples for java.lang.ref WeakReference WeakReference

Introduction

In this page you can find the example usage for java.lang.ref WeakReference WeakReference.

Prototype

public WeakReference(T referent) 

Source Link

Document

Creates a new weak reference that refers to the given object.

Usage

From source file:org.Cherry.Modules.Web.Engine.BadJujuRequestHandlerService.java

@Override
public void handle(final RequestCommand command) throws HttpException, IOException {
    if (command.isBadJuju()) {
        final WeakReference<FastStringWriter> writer = new WeakReference<FastStringWriter>(
                new FastStringWriter());

        getFreeMarkerService().process(normalizeTemplatePath(command.getStatusCode().asURI()), createModel(),
                writer.get());//from  w ww . j  a  va2 s.  co m
        final AbstractHttpEntity entity = new ByteArrayEntity(writer.get().getBuffer().toString().getBytes(),
                APPLICATION_HTML_CONTENT_TYPE);

        command.getResponse().setStatusCode(HttpStatus.SC_OK);
        command.getResponse().setEntity(entity);
    }
}

From source file:com.almende.eve.instantiation.HibernationHandler.java

@Override
public void update(final Handler<T> newHandler) {
    this.referent = new WeakReference<T>(newHandler.get());

    // Can this be done in a cleaner way?
    if (newHandler instanceof HibernationHandler) {
        final HibernationHandler<T> other = (HibernationHandler<T>) newHandler;
        this.wakeKey = other.getWakeKey();
        synchronized (wakeLock) {
            wakeLock.notifyAll();/* w w  w .j a v a 2  s.  c  o m*/
        }
    }
}

From source file:org.commonjava.util.jhttpc.INTERNAL.conn.TrackedHttpClient.java

@Override
protected CloseableHttpResponse doExecute(HttpHost target, HttpRequest request, HttpContext context)
        throws IOException, ClientProtocolException {
    requests.add(new WeakReference<HttpRequest>(request));

    CloseableHttpResponse response = delegate.execute(target, request, context);
    responses.add(new WeakReference<CloseableHttpResponse>(response));

    return response;
}

From source file:com.almende.eve.test.TestWake.java

/**
 * Test wake example.// w  w  w  .j  a  v a  2 s  .  co m
 */
@Test
public void testWake() {

    final AgentConfig config = new AgentConfig("testWakeAgent");

    // First we need to setup the WakeService: (Either keep a global pointer
    // to the wake service, or obtain it again through the same
    // configuration)
    final InstantiationServiceConfig isConfig = new InstantiationServiceConfig();
    final FileStateConfig stateconfig = new FileStateConfig();
    stateconfig.setPath(".wakeservices");
    stateconfig.setId("testWakeService");
    isConfig.setState(stateconfig);

    config.setInstantiationService(isConfig);

    final HttpTransportConfig transConfig = new HttpTransportConfig();
    transConfig.setServletUrl("http://localhost:8080/agents/");
    transConfig.setServletLauncher("JettyLauncher");
    transConfig.setServletClass(DebugServlet.class.getName());
    final ObjectNode jettyParms = JOM.createObjectNode();
    jettyParms.put("port", 8080);
    transConfig.set("jetty", jettyParms);
    config.addTransport(transConfig);

    // Now create a WakeAble Agent
    WeakReference<Agent> test = new WeakReference<Agent>(new MyAgent(config));

    // after a while the agent is unloaded:
    System.gc();
    System.gc();
    try {
        Thread.sleep(1000);
    } catch (final InterruptedException e) {
        e.printStackTrace();
    }
    System.gc();
    System.gc();

    // By now the agent should be unloaded:
    assertNull(test.get());

    // Now some other agent calls the agent:
    new Agent("other", null) {
        public void test() {
            try {
                call(URI.create("local:testWakeAgent"), "helloWorld", null, new AsyncCallback<String>() {

                    @Override
                    public void onSuccess(String result) {
                        called.value = true;
                    }

                    @Override
                    public void onFailure(Exception exception) {
                        fail();
                    }

                });
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }.test();

    // Give the async call time to reach the agent (and wake the agent in
    // the proces);
    try {
        Thread.sleep(1000);
    } catch (final InterruptedException e) {
        e.printStackTrace();
    }
    assertTrue(called.value);
}

From source file:it.feio.android.omninotes.async.UpdaterTask.java

public UpdaterTask(Activity mActivity) {
    this.mActivityReference = new WeakReference<Activity>(mActivity);
    this.mActivity = mActivity;
}

From source file:com.dycody.android.idealnote.async.MainMenuTask.java

public MainMenuTask(Fragment mFragment) {
    mFragmentWeakReference = new WeakReference<>(mFragment);
    this.mainActivity = (MainActivity) mFragment.getActivity();
    ButterKnife.bind(this, mFragment.getView());
}

From source file:android.support.v7.widget.XpAppCompatCompoundDrawableHelper.java

public XpAppCompatCompoundDrawableHelper(@NonNull TextView view) {
    mView = view;//ww w.jav  a  2 s . com
    mDrawableManager = AppCompatDrawableManager.get();

    for (int i = 0, count = 6; i < count; i++) {
        mDrawables.put(i, new WeakReference<Drawable>(null));
    }
}

From source file:com.example.marcieltorres.nfcproject_serverapp.cardreader.LoyaltyCardReader.java

public LoyaltyCardReader(AccountCallback accountCallback) {
    mAccountCallback = new WeakReference<AccountCallback>(accountCallback);
}

From source file:edu.cornell.mannlib.vitro.webapp.utils.threads.VitroBackgroundThread.java

public VitroBackgroundThread(Runnable target, String name) {
    super(target, name);
    allThreads.add(new WeakReference<VitroBackgroundThread>(this));
}

From source file:mitm.common.scheduler.HTTPMethodAbortTimeoutTask.java

public HTTPMethodAbortTimeoutTask(HttpMethod httpMethod, String name) {
    super(name);// www  .j  a v a  2s .c o  m

    Check.notNull(httpMethod, "httpMethod");

    this.weakHttpMethod = new WeakReference<HttpMethod>(httpMethod);
}