下面列出了怎么用com.google.inject.OutOfScopeException的API类实例代码及写法,或者点击链接到github查看源代码。
private <T> Map<Key<?>, Object> getScopedObjectMap(Key<T> key)
{
Map<Key<?>, Object> scopedObjects = values.get();
if (scopedObjects == null)
{
Annotation annotation = key.getAnnotation();
Class<? extends Annotation> annotationType = key.getAnnotationType();
boolean isErrorReporterContext = annotation != null && (annotation instanceof ErrorReporterContext);
isErrorReporterContext |= annotationType != null && annotationType.equals(ErrorReporterContext.class);
if (!isErrorReporterContext)
{
throw new OutOfScopeException(
"Cannot access " + key + " outside of a scoping block"
);
}
}
return scopedObjects;
}
String[] renderedTags() {
final StringBuilder sb = new StringBuilder();
boolean some = false;
for(Provider<Tagger> provider : taggers.get()) {
final Tagger tagger;
try {
tagger = Injection.unwrappingExceptions(OutOfScopeException.class, provider);
} catch(OutOfScopeException e) {
// If the tagger is out of scope, just omit its tags,
// but log a warning in case this hides an unexpected exception.
logger.warning("Ignoring out-of-scope tagger (" + e.toString() + ")");
continue;
}
final ImmutableSet<Tag> tags = tagger.tags();
if(!tags.isEmpty()) {
if(some) sb.append(',');
some = true;
sb.append(tagSetCache.getUnchecked(tags));
}
}
return some ? new String[] {sb.toString()} : EMPTY;
}
@Override
@Nullable
public ClientInfo getCurrentThreadClientInfo() {
try {
HttpServletRequest request = httpRequestProvider.get();
return ClientInfo
.builder()
.userId(UserIdHelper.get())
.remoteIP(request.getRemoteAddr())
.userAgent(request.getHeader(HttpHeaders.USER_AGENT))
.path(request.getServletPath())
.build();
}
catch (ProvisionException | OutOfScopeException e) {
// ignore; this happens when called out of scope of http request
return null;
}
}
/**
* Derives Key (Service Key + Window) to use in Redis for noOfReq limiting.
* Recommended to use perMillis to lowest granularity. i.e, to seconds. 1000, 2000
* <p>
* Depends on perMillis
* <p>
* - perMillis < 1000 : Window considered for milliseconds
* - perMillis >=1000 && <=60000 : Window considered for seconds(s)
*
* @return new key based on perMillis (works for second/minute/hour windows only)
*/
private String getKeyForWindow(String key) throws OutOfScopeException {
LocalDateTime now = LocalDateTime.now();
int window;
if (perMillis >= 1 && perMillis < 1000) {
window = (now.get(ChronoField.MILLI_OF_DAY) / perMillis) + 1;
} else if (perMillis >= 1000 && perMillis <= 60000) {
window = now.get(ChronoField.SECOND_OF_MINUTE) / (perMillis / 1000);
} else {
throw new OutOfScopeException("perMillis specified is not currently supported");
}
return key + window;
}
@Override
public <T> Provider<T> scope(final Key<T> key, final Provider<T> unscoped) {
return () -> {
if (scope == null) {
throw new OutOfScopeException("Not in scope");
}
Object value = scope.get(key);
if (value == null) {
T provided = unscoped.get();
if (provided instanceof CircularDependencyProxy) {
return provided;
}
value = (provided == null) ? NULL_SENTINEL : provided;
scope.put(key, value);
}
@SuppressWarnings("unchecked")
T result = (value != NULL_SENTINEL) ? (T) value : null;
return result;
};
}
@Override
public <T> Provider<T> scope(Key<T> key, Provider<T> unscoped) {
return () -> {
final InjectionStore<A> store = currentStore(key).orElseThrow(
() -> new OutOfScopeException("Cannot provide " + key + " outside of " + annotation.getSimpleName() + " scope")
);
// If the current store already contains the key, return its value.
// Otherwise, provision an unscoped value and add it to the store.
// If the key is of the store itself, just use the store we have.
return store.provide(key, () -> storeKey.equals(key) ? (T) store : unscoped.get());
};
}
public ScopedObjects getScope() {
Stack<ScopedObjects> scope = scopeLocal.get();
if (scope == null) {
throw new OutOfScopeException("Not inside an ExecutionScope");
}
return scope.peek();
}
@Override
public <T> Provider<T> scope(Key<T> key, Provider<T> unscoped) {
return () -> {
Context context = contextRef.get();
if (context != null) {
return context.get(key, unscoped);
} else {
throw new OutOfScopeException("Not currently in a scope");
}
};
}
private <T> Map<Key<?>, Object> getScopedObjectMap(Key<T> key) {
Map<Key<?>, Object> scopedObjects = THREAD_LOCAL.get();
if (scopedObjects == null) {
throw new OutOfScopeException("Cannot access " + key
+ " outside of a scoping block");
}
return scopedObjects;
}
private <T> Map<Key<?>, Object> getScopedObjectMap(Key<T> key) {
AtomicReference<Map<Key<?>, Object>> mapHolder = values.get();
if (mapHolder == null || mapHolder.get() == null) {
throw new OutOfScopeException("Attempt to inject @TestScoped binding outside test: " + key);
}
return mapHolder.get();
}
@Test(expected = OutOfScopeException.class)
public void testProviderNotEnteredScope() {
scope.scope(key, unscopedProvider).get();
}
@Test(expected = OutOfScopeException.class)
public void testSeedNotEnteredScope() {
scope.seed(key, seed);
}
public Model processModel(Model projectModel, Map<String, ?> options) throws IOException {
if (this.disabled) {
return projectModel;
}
final Source pomSource = (Source) options.get(ModelProcessor.SOURCE);
if (pomSource != null) {
projectModel.setPomFile(new File(pomSource.getLocation()));
}
try {
if (!initialized) {
logger.info("");
String extensionId = BuildProperties.projectArtifactId() + ":" + BuildProperties.projectVersion();
logger.info(extensionLogFormat(extensionId));
try {
mavenSession = sessionScope.scope(Key.get(MavenSession.class), null).get();
} catch (OutOfScopeException ex) {
logger.warn("skip - no maven session present");
disabled = true;
return projectModel;
}
if (parseBoolean(getCommandOption(OPTION_NAME_DISABLE))) {
logger.info("skip - versioning is disabled");
disabled = true;
return projectModel;
}
File executionRootDirectory = new File(mavenSession.getRequest().getBaseDirectory());
logger.debug("executionRootDirectory: " + executionRootDirectory.toString());
mvnDirectory = findMvnDirectory(executionRootDirectory);
logger.debug("mvnDirectory: " + mvnDirectory.toString());
String configFileName = BuildProperties.projectArtifactId() + ".xml";
File configFile = new File(mvnDirectory, configFileName);
logger.debug("configFile: " + configFile.toString());
config = loadConfig(configFile);
gitDirectory = findGitDir(executionRootDirectory);
if (gitDirectory == null || !gitDirectory.exists()) {
logger.warn("skip - project is not part of a git repository");
disabled = true;
return projectModel;
}
logger.debug("gitDirectory: " + gitDirectory.toString());
gitVersionDetails = getGitVersionDetails(config, executionRootDirectory);
logger.info("Adjusting project models...");
logger.info("");
initialized = true;
}
return processModel(projectModel);
} catch (Exception e) {
throw new IOException("Git Versioning Model Processor", e);
}
}