com.google.inject.OutOfScopeException Java Examples

The following examples show how to use com.google.inject.OutOfScopeException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LinStorScope.java    From linstor-server with GNU General Public License v3.0 6 votes vote down vote up
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;
}
 
Example #2
Source File: DataDogClient.java    From ProjectAres with GNU Affero General Public License v3.0 6 votes vote down vote up
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;
}
 
Example #3
Source File: ClientInfoProviderImpl.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@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;
  }
}
 
Example #4
Source File: RedisRateLimiter.java    From pay-publicapi with MIT License 6 votes vote down vote up
/**
 * 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;
}
 
Example #5
Source File: ExplicitScope.java    From js-dossier with Apache License 2.0 6 votes vote down vote up
@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;
  };
}
 
Example #6
Source File: InjectionScope.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
@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());
    };
}
 
Example #7
Source File: ExecutionScoper.java    From yql-plus with Apache License 2.0 5 votes vote down vote up
public ScopedObjects getScope() {
    Stack<ScopedObjects> scope = scopeLocal.get();
    if (scope == null) {
        throw new OutOfScopeException("Not inside an ExecutionScope");
    }
    return scope.peek();
}
 
Example #8
Source File: BiomedicusScopes.java    From biomedicus with Apache License 2.0 5 votes vote down vote up
@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");
    }
  };
}
 
Example #9
Source File: SimpleScope.java    From datakernel with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #10
Source File: TestScope.java    From acai with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example #11
Source File: ServiceRequestScopeImplTest.java    From act-platform with ISC License 4 votes vote down vote up
@Test(expected = OutOfScopeException.class)
public void testProviderNotEnteredScope() {
  scope.scope(key, unscopedProvider).get();
}
 
Example #12
Source File: ServiceRequestScopeImplTest.java    From act-platform with ISC License 4 votes vote down vote up
@Test(expected = OutOfScopeException.class)
public void testSeedNotEnteredScope() {
  scope.seed(key, seed);
}
 
Example #13
Source File: GitVersioningModelProcessor.java    From maven-git-versioning-extension with MIT License 4 votes vote down vote up
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);
    }
}