Last active
December 17, 2015 10:08
-
-
Save mbruch/5592142 to your computer and use it in GitHub Desktop.
Proxy OSGI Service zu HTTP Server für das Client Plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.miniscout.client; | |
import java.util.Dictionary; | |
import java.util.Hashtable; | |
import org.eclipse.ui.plugin.AbstractUIPlugin; | |
import org.osgi.framework.BundleContext; | |
import org.osgi.framework.Constants; | |
import com.example.miniscout.client.services.LocalDatabaseService; | |
import com.example.miniscout.client.services.ProxyDatabaseService; | |
import com.example.miniscout.shared.IDatabaseService; | |
public class Activator extends AbstractUIPlugin { | |
private static Activator plugin; | |
public void start(BundleContext context) throws Exception { | |
super.start(context); | |
plugin = this; | |
context.registerService(IDatabaseService.class, new LocalDatabaseService(), | |
null); | |
// Register a proxy service with a higher priority: | |
Dictionary<String, Object> props = new Hashtable<>(); | |
props.put(Constants.SERVICE_RANKING, 1000); | |
context.registerService(IDatabaseService.class, new ProxyDatabaseService(), | |
props); | |
} | |
public void stop(BundleContext context) throws Exception { | |
plugin = null; | |
super.stop(context); | |
} | |
public static Activator getDefault() { | |
return plugin; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.miniscout.client.services; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.net.URI; | |
import com.example.miniscout.shared.Form; | |
import com.example.miniscout.shared.IDatabaseService; | |
import com.google.common.base.Throwables; | |
import com.google.common.io.CharStreams; | |
import com.google.gson.Gson; | |
public class ProxyDatabaseService implements IDatabaseService { | |
@Override | |
public Form load(URI source) { | |
try { | |
if (!source.getScheme().startsWith("http")) { | |
// we trick a bit. use hardcoded urls for demo purpose: | |
// change for to the appropriate URL | |
source = new URI("http://localhost:8080/form"); | |
} | |
InputStream stream = source.toURL().openStream(); | |
String form = CharStreams.toString(new InputStreamReader(stream)); | |
return new Gson().fromJson(form, Form.class); | |
} catch (Exception e) { | |
throw Throwables.propagate(e); | |
} | |
} | |
@Override | |
public void save(Form form, URI target) { | |
// we don't save implement save. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment