Last active
December 17, 2015 16:49
-
-
Save wsky/5641097 to your computer and use it in GitHub Desktop.
context base service design, resolve special runtime/environment dependency
This file contains hidden or 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
// context interface to resolve env dependency | |
public interface ContextService { | |
public Object get(String key); | |
} | |
// context base | |
// alose can impl via aop inspector | |
public class BizServiceAdapter implements BizInterface { | |
private ContextService context; | |
private BizService bizService; | |
public BizServiceAdapter(ContextService context, BizService bizService) { | |
this.context = context; | |
this.bizService = bizService; | |
} | |
public String echo(String input) { | |
if (context.get("appkey") == null) | |
throw new NullPointerException("Not Auth"); | |
return this.bizService.echo(input); | |
} | |
} | |
// origin service | |
public class BizService implements BizInterface { | |
public String echo(String input) { | |
return input; | |
} | |
} | |
public interface BizInterface { | |
public String echo(String input); | |
} | |
//runtime/env based context impl | |
public class RemotingContextService implements ContextService { | |
@Override | |
public Object get(String key) { | |
// can get from env-var/threadlocal/per-request | |
return System.getProperty(key); | |
} | |
} | |
public class HttpContextService implements ContextService { | |
@Override | |
public Object get(String key) { | |
return System.getProperty(key); | |
} | |
} | |
public class HsfContextService implements ContextService { | |
@Override | |
public Object get(String key) { | |
return System.getProperty(key); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment