private void enqueueHealthAndNewTaskChecks() { final long start = System.currentTimeMillis(); final List<SingularityTask> activeTasks = taskManager.getActiveTasks(); final Map<SingularityTaskId, SingularityTask> activeTaskMap = Maps.uniqueIndex(activeTasks, SingularityTaskIdHolder.getTaskIdFunction()); final Map<SingularityTaskId, List<SingularityTaskHistoryUpdate>> taskUpdates = taskManager.getTaskHistoryUpdates(activeTaskMap.keySet()); final Map<SingularityDeployKey, SingularityPendingDeploy> pendingDeploys = Maps.uniqueIndex( deployManager.getPendingDeploys(), SingularityDeployKey.FROM_PENDING_TO_DEPLOY_KEY); final Map<String, SingularityRequestWithState> idToRequest = Maps.uniqueIndex( requestManager.getRequests(), SingularityRequestWithState.REQUEST_STATE_TO_REQUEST_ID); requestManager.getActiveRequests(); int enqueuedNewTaskChecks = 0; int enqueuedHealthchecks = 0; for (Map.Entry<SingularityTaskId, SingularityTask> entry : activeTaskMap.entrySet()) { SingularityTaskId taskId = entry.getKey(); SingularityTask task = entry.getValue(); SimplifiedTaskState simplifiedTaskState = SingularityTaskHistoryUpdate.getCurrentState(taskUpdates.get(taskId)); if (simplifiedTaskState != SimplifiedTaskState.DONE) { SingularityDeployKey deployKey = new SingularityDeployKey(taskId.getRequestId(), taskId.getDeployId()); Optional<SingularityPendingDeploy> pendingDeploy = Optional.fromNullable(pendingDeploys.get(deployKey)); Optional<SingularityRequestWithState> request = Optional.fromNullable(idToRequest.get(taskId.getRequestId())); if (!pendingDeploy.isPresent()) { newTaskChecker.enqueueNewTaskCheck(task, request, healthchecker); enqueuedNewTaskChecks++; } if (simplifiedTaskState == SimplifiedTaskState.RUNNING) { if (healthchecker.enqueueHealthcheck(task, pendingDeploy, request)) { enqueuedHealthchecks++; } } } } LOG.info( "Enqueued {} health checks and {} new task checks (out of {} active tasks) in {}", enqueuedHealthchecks, enqueuedNewTaskChecks, activeTasks.size(), JavaUtils.duration(start)); }
private Map<String, MeasureDto> measuresByMetricKey(ComponentDto component, DbSession session) { MeasureQuery query = MeasureQuery.builder() .setComponentUuid(component.uuid()) .setMetricKeys(METRIC_KEYS) .build(); List<MeasureDto> measures = dbClient.measureDao().selectByQuery(session, query); Set<Integer> metricIds = measures.stream().map(MeasureDto::getMetricId).collect(Collectors.toSet()); List<MetricDto> metrics = dbClient.metricDao().selectByIds(session, metricIds); Map<Integer, MetricDto> metricsById = Maps.uniqueIndex(metrics, MetricDto::getId); return Maps.uniqueIndex(measures, m -> metricsById.get(m.getMetricId()).getKey()); }
private Table<String, MetricDto, MeasureDto> searchMeasuresByComponentUuidAndMetric( DbSession dbSession, ComponentDto baseComponent, List<ComponentDto> components, List<MetricDto> metrics, List<WsMeasures.Period> periods, @Nullable Long developerId) { List<String> componentUuids = new ArrayList<>(); componentUuids.add(baseComponent.uuid()); components.stream().forEach(c -> componentUuids.add(c.uuid())); Map<Integer, MetricDto> metricsById = Maps.uniqueIndex(metrics, MetricDtoFunctions.toId()); MeasureQuery measureQuery = MeasureQuery.builder() .setPersonId(developerId) .setComponentUuids(componentUuids) .setMetricIds(metricsById.keySet()) .build(); List<MeasureDto> measureDtos = dbClient.measureDao().selectByQuery(dbSession, measureQuery); Table<String, MetricDto, MeasureDto> measuresByComponentUuidAndMetric = HashBasedTable.create(components.size(), metrics.size()); for (MeasureDto measureDto : measureDtos) { measuresByComponentUuidAndMetric.put( measureDto.getComponentUuid(), metricsById.get(measureDto.getMetricId()), measureDto); } addBestValuesToMeasures(measuresByComponentUuidAndMetric, components, metrics, periods); return measuresByComponentUuidAndMetric; }
public List<CashOutEntry> getBlingCashOutRequests() throws ServiceException { Map<Integer, CashOutInfo> blingMap = _moneyLogic.getBlingCashOutRequests(); // Get all member names for the members in the map. final Map<Integer, MemberRecord> names = Maps.uniqueIndex( _memberRepo.loadMembers(blingMap.keySet()), new Function<MemberRecord, Integer>() { public Integer apply(MemberRecord record) { return record.memberId; } }); // Get list of charities final List<CharityRecord> charities = _memberRepo.getCharities(); // Transform the list into cash out entries. return Lists.newArrayList( Iterables.transform( blingMap.entrySet(), new Function<Map.Entry<Integer, CashOutInfo>, CashOutEntry>() { public CashOutEntry apply(Entry<Integer, CashOutInfo> entry) { MemberRecord member = names.get(entry.getKey()); return new CashOutEntry( entry.getKey(), member.getName().getNormal(), entry.getValue(), member.accountName, isCharity(member.memberId, charities)); } })); }
/** * Specifies how this resource maps to underlying resourcesIf the HostResource array contains any * entries, this property reflects how the resource maps to those specific resources. */ public static enum MappingBehavior { UNKNOWN(0), NOT_SUPPORTED(2), DEDICATED(3), SOFT_AFFINITY(4), HARD_AFFINITY(5), DMTF_RESERVED(32767), VENDOR_RESERVED(65535); protected final int code; MappingBehavior(int code) { this.code = code; } public String value() { return code + ""; } protected static final Map<Integer, MappingBehavior> MAPPING_BEHAVIOR_BY_ID = Maps.uniqueIndex( ImmutableSet.copyOf(MappingBehavior.values()), new Function<MappingBehavior, Integer>() { @Override public Integer apply(MappingBehavior input) { return input.code; } }); public static MappingBehavior fromValue(String behavior) { return MAPPING_BEHAVIOR_BY_ID.get(new Integer(checkNotNull(behavior, "behavior"))); } }
/** * This enum maps the commands for the <a href= * "http://dragonfly.opera.com/app/scope-interface/services/Exec/Exec_2_0.html" >Exec 2.0</a>. */ public enum ExecMessage implements Message { EXEC(1), GET_ACTION_LIST(2), SETUP_SCREEN_WATCHER(3), SEND_MOUSE_ACTION(5), DEFAULT(-1); private static final Map<Integer, ExecMessage> lookup = Maps.uniqueIndex( ImmutableList.copyOf(values()), new Function<ExecMessage, Integer>() { public Integer apply(ExecMessage message) { return message.getID(); } }); private final int code; private ExecMessage(int code) { this.code = code; } public int getID() { return code; } public String getServiceName() { return Exec.SERVICE_NAME; } public static ExecMessage get(int code) { ExecMessage message = lookup.get(code); return (message != null) ? message : DEFAULT; } }
public static String printLogical(List<PlanFragment> fragments) { Map<PlanFragmentId, PlanFragment> fragmentsById = Maps.uniqueIndex( fragments, new Function<PlanFragment, PlanFragmentId>() { @Override public PlanFragmentId apply(PlanFragment input) { return input.getId(); } }); PlanNodeIdGenerator idGenerator = new PlanNodeIdGenerator(); StringBuilder output = new StringBuilder(); output.append("digraph logical_plan {\n"); for (PlanFragment fragment : fragments) { printFragmentNodes(output, fragment, idGenerator); } for (PlanFragment fragment : fragments) { fragment.getRoot().accept(new EdgePrinter(output, fragmentsById, idGenerator), null); } output.append("}\n"); return output.toString(); }
@Override public void setGroupMapping(Map<String, String> mapping) { Map<String, String> internal; if (mapping == null) { internal = Collections.emptyMap(); } else { // we store ids internally but external users use the group names try { final Map<String, Role> nameToRole = Maps.uniqueIndex(roleService.loadAll(), Roles.roleToNameFunction()); internal = Maps.newHashMap( Maps.transformValues( mapping, new Function<String, String>() { @Nullable @Override public String apply(@Nullable String groupName) { if (groupName == null || !nameToRole.containsKey(groupName)) { return null; } return nameToRole.get(groupName).getId(); } })); } catch (NotFoundException e) { LOG.error("Unable to convert group names to ids", e); throw new IllegalStateException("Unable to convert group names to ids", e); } } fields.put(GROUP_MAPPING, internal); }
private void blockUntilRunningAndAssignElasticIpsToInstancesOrPutIntoBadMap( Set<RunningInstance> input, Map<NodeMetadata, Exception> badNodes) { Map<RegionAndName, RunningInstance> instancesById = Maps.uniqueIndex(input, instanceToRegionAndName); for (Map.Entry<RegionAndName, RunningInstance> entry : instancesById.entrySet()) { RegionAndName id = entry.getKey(); RunningInstance instance = entry.getValue(); try { logger.debug("<< allocating elastic IP instance(%s)", id); String ip = client.getElasticIPAddressServices().allocateAddressInRegion(id.getRegion()); // block until instance is running logger.debug(">> awaiting status running instance(%s)", id); AtomicReference<NodeMetadata> node = newReference(runningInstanceToNodeMetadata.apply(instance)); nodeRunning.apply(node); logger.trace("<< running instance(%s)", id); logger.debug(">> associating elastic IP %s to instance %s", ip, id); client .getElasticIPAddressServices() .associateAddressInRegion(id.getRegion(), ip, id.getName()); logger.trace("<< associated elastic IP %s to instance %s", ip, id); // add mapping of instance to ip into the cache elasticIpCache.put(id, ip); } catch (RuntimeException e) { badNodes.put(runningInstanceToNodeMetadata.apply(instancesById.get(id)), e); } } }
@Override public void setAdditionalDefaultGroups(Set<String> groupNames) { try { if (groupNames == null) return; final Map<String, Role> nameToRole = Maps.uniqueIndex(roleService.loadAll(), Roles.roleToNameFunction()); final List<String> groupIds = Lists.newArrayList( Collections2.transform( groupNames, new Function<String, String>() { @Nullable @Override public String apply(@Nullable String groupName) { if (groupName == null || !nameToRole.containsKey(groupName)) { return null; } return nameToRole.get(groupName).getId(); } })); fields.put(ADDITIONAL_DEFAULT_GROUPS, groupIds); } catch (NotFoundException e) { LOG.error("Unable to convert group names to ids", e); throw new IllegalStateException("Unable to convert group names to ids", e); } }
private static String generateHelpText() { Map<Integer, String> details = Maps.transformValues( Maps.uniqueIndex(RESPONSE_CODES.values(), Pair::getFirst), Pair::getSecond); return "This endpoint can indicate to a load balancer which among a group of schedulers " + "is leading.\n\nThe response codes are:\n" + Joiner.on("\n").withKeyValueSeparator(": ").join(details); }
public NetconfMessageTransformer(final SchemaContext schemaContext, final boolean strictParsing) { this.counter = new MessageCounter(); this.schemaContext = schemaContext; parserFactory = DomToNormalizedNodeParserFactory.getInstance( XmlUtils.DEFAULT_XML_CODEC_PROVIDER, schemaContext, strictParsing); mappedRpcs = Maps.uniqueIndex(schemaContext.getOperations(), QNAME_FUNCTION); mappedNotifications = Multimaps.index(schemaContext.getNotifications(), QNAME_NOREV_FUNCTION); }
@Override public Map<NamedKey, byte[]> getBulk(Iterable<NamedKey> keys) { try (ResourceHolder<MemcachedClientIF> clientHolder = client.get()) { Map<String, NamedKey> keyLookup = Maps.uniqueIndex( keys, new Function<NamedKey, String>() { @Override public String apply(@Nullable NamedKey input) { return computeKeyHash(memcachedPrefix, input); } }); Map<NamedKey, byte[]> results = Maps.newHashMap(); BulkFuture<Map<String, Object>> future; try { future = clientHolder.get().asyncGetBulk(keyLookup.keySet()); } catch (IllegalStateException e) { // operation did not get queued in time (queue is full) errorCount.incrementAndGet(); log.warn(e, "Unable to queue cache operation"); return results; } try { Map<String, Object> some = future.getSome(timeout, TimeUnit.MILLISECONDS); if (future.isTimeout()) { future.cancel(false); timeoutCount.incrementAndGet(); } missCount.addAndGet(keyLookup.size() - some.size()); hitCount.addAndGet(some.size()); for (Map.Entry<String, Object> entry : some.entrySet()) { final NamedKey key = keyLookup.get(entry.getKey()); final byte[] value = (byte[]) entry.getValue(); if (value != null) { results.put(key, deserializeValue(key, value)); } } return results; } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw Throwables.propagate(e); } catch (ExecutionException e) { errorCount.incrementAndGet(); log.warn(e, "Exception pulling item from cache"); return results; } } }
@Test public void testFindAll() { // load users from db Map<String, User> resultMap = Maps.uniqueIndex(userRepository.findAll(), userName()); assertThat(resultMap).hasSameSizeAs(userMap.values()); // verify for (User user : userMap.values()) { assertThat(user).is(sameAsUser(resultMap.get(user.getUserName()))); } }
public void addConnectorSessionProperties( ConnectorId connectorId, List<PropertyMetadata<?>> properties) { requireNonNull(connectorId, "connectorId is null"); requireNonNull(properties, "properties is null"); Map<String, PropertyMetadata<?>> propertiesByName = Maps.uniqueIndex(properties, PropertyMetadata::getName); checkState( connectorSessionProperties.putIfAbsent(connectorId, propertiesByName) == null, "Session properties for connectorId '%s' are already registered", connectorId); }
/** * Filters SchemaContext for yang modules * * @param delegate original SchemaContext * @param rootModules modules (yang schemas) to be available and all their dependencies (modules * importing rootModule and whole chain of their imports) * @param additionalModuleIds (additional) modules (yang schemas) to be available and whole chain * of their imports */ public FilteringSchemaContextProxy( final SchemaContext delegate, final Collection<ModuleId> rootModules, final Set<ModuleId> additionalModuleIds) { Preconditions.checkArgument(rootModules != null, "Base modules cannot be null."); Preconditions.checkArgument(additionalModuleIds != null, "Additional modules cannot be null."); final Builder<Module> filteredModulesBuilder = new Builder<>(); final SetMultimap<URI, Module> nsMap = Multimaps.newSetMultimap(new TreeMap<URI, Collection<Module>>(), MODULE_SET_SUPPLIER); final SetMultimap<String, Module> nameMap = Multimaps.newSetMultimap(new TreeMap<String, Collection<Module>>(), MODULE_SET_SUPPLIER); ImmutableMap.Builder<ModuleIdentifier, String> identifiersToSourcesBuilder = ImmutableMap.builder(); // preparing map to get all modules with one name but difference in revision final TreeMultimap<String, Module> nameToModulesAll = getStringModuleTreeMultimap(); nameToModulesAll.putAll(getStringModuleMap(delegate)); // in case there is a particular dependancy to view filteredModules/yang models // dependancy is checked for module name and imports processForRootModules(delegate, rootModules, filteredModulesBuilder); // adding additional modules processForAdditionalModules(delegate, additionalModuleIds, filteredModulesBuilder); filteredModulesBuilder.addAll( getImportedModules( Maps.uniqueIndex(delegate.getModules(), ModuleId.MODULE_TO_MODULE_ID), filteredModulesBuilder.build(), nameToModulesAll)); /** * Instead of doing this on each invocation of getModules(), pre-compute it once and keep it * around -- better than the set we got in. */ this.filteredModules = filteredModulesBuilder.build(); for (final Module module : filteredModules) { nameMap.put(module.getName(), module); nsMap.put(module.getNamespace(), module); identifiersToSourcesBuilder.put(module, module.getSource()); } namespaceToModules = ImmutableSetMultimap.copyOf(nsMap); nameToModules = ImmutableSetMultimap.copyOf(nameMap); identifiersToSources = identifiersToSourcesBuilder.build(); }
@Override public Map<Integer, DataApprovalLevel> getDataApprovalLevelMap() { List<DataApprovalLevel> levels = dataApprovalLevelStore.getAllDataApprovalLevels(); return Maps.uniqueIndex( levels, new Function<DataApprovalLevel, Integer>() { @Override public Integer apply(DataApprovalLevel level) { return level.getLevel(); } }); }
/** * Creates a new vocabulary backed by the given {@link Enum} class and with properties having the * common URI stem <code>base</code> and prefix <code>prefix</code> * * @param clazz the enumeration backing this vocabulary. * @param base the common stem URI of properties in this vocabulary. * @param prefix the common prefix of properties in this vocabulary. */ public <T extends Enum<T>> EnumVocab( final Class<T> clazz, final String base, final String prefix) { this.index = ImmutableMap.copyOf( Maps.transformEntries( Maps.uniqueIndex(EnumSet.allOf(clazz), ENUM_TO_NAME), new EntryTransformer<String, T, Property>() { @Override public Property transformEntry(String name, T enumee) { return Property.newFrom(name, base, prefix, enumee); } })); }
public static String printDistributed(SubPlan plan) { List<PlanFragment> fragments = plan.getAllFragments(); Map<PlanFragmentId, PlanFragment> fragmentsById = Maps.uniqueIndex(fragments, PlanFragment.idGetter()); PlanNodeIdGenerator idGenerator = new PlanNodeIdGenerator(); StringBuilder output = new StringBuilder(); output.append("digraph distributed_plan {\n"); printSubPlan(plan, fragmentsById, idGenerator, output); output.append("}\n"); return output.toString(); }
public void set(Collection<Comment> comments, Map<Long, Vote> votes, String op) { ImmutableMap<Long, Comment> byId = Maps.uniqueIndex(comments, Comment::getId); this.op = Optional.fromNullable(op); this.comments = FluentIterable.from(sort(comments, op)) .transform( comment -> { int depth = getCommentDepth(byId, comment); Vote baseVote = firstNonNull(votes.get(comment.getId()), Vote.NEUTRAL); return new CommentEntry(comment, baseVote, depth); }) .toList(); notifyDataSetChanged(); }
/** Describes the consumers visibility to the allocated resource. */ public static enum ConsumerVisibility { UNKNOWN(0), /** * indicates the underlying or host resource is utilized and passed through to the consumer, * possibly using partitioning. At least one item shall be present in the HostResource property. */ PASSED_THROUGH(2), /** * indicates the resource is virtualized and may not map directly to an underlying/host * resource. Some implementations may support specific assignment for virtualized resources, in * which case the host resource(s) are exposed using the HostResource property. */ VIRTUALIZED(3), /** * indicates a representation of the resource does not exist within the context of the resource * consumer. */ NOT_REPRESENTED(4), DMTF_RESERVED(32767), VENDOR_RESERVED(65535); protected final int code; ConsumerVisibility(int code) { this.code = code; } public String value() { return code + ""; } protected static final Map<Integer, ConsumerVisibility> MAPPING_BEHAVIOR_BY_ID = Maps.uniqueIndex( ImmutableSet.copyOf(ConsumerVisibility.values()), new Function<ConsumerVisibility, Integer>() { @Override public Integer apply(ConsumerVisibility input) { return input.code; } }); public static ConsumerVisibility fromValue(String behavior) { return MAPPING_BEHAVIOR_BY_ID.get(new Integer(checkNotNull(behavior, "behavior"))); } }
private Map<String, SourcedDeviceUpnp> convertToSourcedDimmableLightMap( Collection<SourcedDeviceUpnp> devices, Source source) { for (SourcedDeviceUpnp device : devices) { device.setSources(EnumSet.of(source)); } return Maps.transformValues( Maps.uniqueIndex( devices, new Function<SourcedDeviceUpnp, String>() { @Override public String apply(SourcedDeviceUpnp device) { return device.getKey(); } }), new SourcedDeviceUpnpConverterFunction(EnumSet.of(source))); }
public static InitAction create( File prideDirectory, RuntimeConfiguration globalConfig, VcsManager vcsManager, boolean force, boolean addExisting, boolean ignoreExistingConfig) throws Exception { boolean prideExistsAlready = Pride.containsPride(prideDirectory); if (prideExistsAlready && !force) { throw new PrideException("A pride already exists in " + prideDirectory); } Configuration existingPrideConfig = new BaseConfiguration(); Map<String, Module> modulesFromExistingPrideConfig = Collections.emptyMap(); if (prideExistsAlready && !ignoreExistingConfig) { try { Pride pride = Pride.getPride(prideDirectory, globalConfig, vcsManager); existingPrideConfig = pride.getLocalConfiguration(); // Get existing modules if (addExisting) { modulesFromExistingPrideConfig = Maps.uniqueIndex( pride.getModules(), new Function<Module, String>() { @Override public String apply(Module module) { return module.getName(); } }); } } catch (Exception ex) { logger.warn("Could not load existing pride, ignoring existing configuration"); logger.debug("Exception was", ex); } } return new InitAction( prideDirectory, globalConfig, existingPrideConfig, vcsManager, modulesFromExistingPrideConfig, addExisting); }
WorkflowSchemeBean asBean(AssignableWorkflowScheme parent, DraftWorkflowScheme child) { final WorkflowSchemeBean bean = new WorkflowSchemeBean(); bean.setName(parent.getName()); bean.setDescription(parent.getDescription()); WorkflowScheme scheme = child != null ? child : parent; bean.setDefaultWorkflow(scheme.getConfiguredDefaultWorkflow()); bean.setIssueTypeMappings(asIssueTypeMap(scheme)); bean.setId(scheme.getId()); bean.setDraft(scheme.isDraft()); bean.setDefaultWorkflow(scheme.getActualDefaultWorkflow()); final IssueTypeBeanBuilder builder = new IssueTypeBeanBuilder().context(info).jiraBaseUrls(jiraBaseUrls); bean.setIssueTypes( Maps.uniqueIndex( Collections2.transform( issueTypeManager.getIssueTypes(), new Function<IssueType, IssueTypeJsonBean>() { @Override public IssueTypeJsonBean apply(IssueType issueType) { return builder.issueType(issueType).build(); } }), new Function<IssueTypeJsonBean, String>() { @Override public String apply(IssueTypeJsonBean issueType) { return issueType.getId(); } })); if (child != null) { bean.setLastModifiedUser(beanForUser(child.getLastModifiedUser())); bean.setLastModified(formatter.forLoggedInUser().format(child.getLastModifiedDate())); bean.setOriginalIssueTypeMappings(asIssueTypeMap(parent)); bean.setSelf(getUrlForParent(parent).path("draft").build()); bean.setOriginalDefaultWorkflow(child.getParentScheme().getActualDefaultWorkflow()); } else { bean.setSelf(getUrlForParent(parent).build()); } return bean; }
/** * The primary loading code * * <p>The found resources are first loaded into the {@link #modClassLoader} (always) then scanned * for class resources matching the specification above. * * <p>If they provide the {@link Mod} annotation, they will be loaded as "FML mods" * * <p>Finally, if they are successfully loaded as classes, they are then added to the available * mod list. */ private ModDiscoverer identifyMods() { FMLLog.fine("Building injected Mod Containers %s", injectedContainers); // Add in the MCP mod container mods.add(new InjectedModContainer(mcp, new File("minecraft.jar"))); for (String cont : injectedContainers) { ModContainer mc; try { mc = (ModContainer) Class.forName(cont, true, modClassLoader).newInstance(); } catch (Exception e) { FMLLog.log( Level.ERROR, e, "A problem occured instantiating the injected mod container %s", cont); throw new LoaderException(e); } mods.add(new InjectedModContainer(mc, mc.getSource())); } ModDiscoverer discoverer = new ModDiscoverer(); FMLLog.fine( "Attempting to load mods contained in the minecraft jar file and associated classes"); discoverer.findClasspathMods(modClassLoader); FMLLog.fine("Minecraft jar mods loaded successfully"); FMLLog.getLogger() .log( Level.INFO, "Found {} mods from the command line. Injecting into mod discoverer", ModListHelper.additionalMods.size()); FMLLog.info("Searching %s for mods", canonicalModsDir.getAbsolutePath()); discoverer.findModDirMods( canonicalModsDir, ModListHelper.additionalMods.values().toArray(new File[0])); File versionSpecificModsDir = new File(canonicalModsDir, mccversion); if (versionSpecificModsDir.isDirectory()) { FMLLog.info("Also searching %s for mods", versionSpecificModsDir); discoverer.findModDirMods(versionSpecificModsDir); } mods.addAll(discoverer.identifyMods()); identifyDuplicates(mods); namedMods = Maps.uniqueIndex(mods, new ModIdFunction()); FMLLog.info( "Forge Mod Loader has identified %d mod%s to load", mods.size(), mods.size() != 1 ? "s" : ""); return discoverer; }
public static enum Relationship { ABOUT("about"), TWITTER_AUDIENCE("twitter:audience"), TWITTER_AUDIENCE_RELATED("twitter:audience-related"), TWITTER_AUDIENCE_REALTIME("twitter:audience:realtime"), TRANSCRIPTION("transcription"), TRANSCRIPTION_SUBTITLES("transcription:subtitles"), TRANSCRIPTION_SUBTITLES_REALTIME("transcription:subtitles:realtime"); private final String name; private Relationship(String name) { this.name = name; } @Override public String toString() { return name; } private static ImmutableSet<Relationship> ALL = ImmutableSet.copyOf(values()); public static ImmutableSet<Relationship> all() { return ALL; } private static ImmutableMap<String, Optional<Relationship>> LOOKUP = ImmutableMap.copyOf( Maps.transformValues( Maps.uniqueIndex(all(), Functions.toStringFunction()), new Function<Relationship, Optional<Relationship>>() { @Override public Optional<Relationship> apply(@Nullable Relationship input) { return Optional.fromNullable(input); } })); public static Optional<Relationship> fromString(String relationship) { Optional<Relationship> possibleRelationship = LOOKUP.get(relationship); return possibleRelationship != null ? possibleRelationship : Optional.<Relationship>absent(); } }
@Test public void releasableReferenceManagers() { ImmutableMap<Class<? extends Annotation>, ReleasableReferenceManager> managers = Maps.uniqueIndex( component.managers(), new Function<ReleasableReferenceManager, Class<? extends Annotation>>() { @Override public Class<? extends Annotation> apply( ReleasableReferenceManager releasableReferenceManager) { return releasableReferenceManager.scope(); } }); assertThat(managers) .containsEntry(ParentReleasableScope1.class, component.parentReleasableScope1Manager()); assertThat(managers) .containsEntry(ParentReleasableScope2.class, component.parentReleasableScope2Manager()); assertThat(managers) .containsEntry(ChildReleasableScope1.class, component.childReleasableScope1Manager()); assertThat(managers) .containsEntry(ChildReleasableScope2.class, component.childReleasableScope2Manager()); // Should contain a manager for ChildReleasableScope3 even though // @ForReleasableReferences(Scope5.class) isn't needed. assertThat(managers).containsKey(ChildReleasableScope3.class); }
@XmlType @XmlEnum(String.class) public static enum ImageType { @XmlEnumValue("iso") ISO("iso"), @XmlEnumValue("floppy") FLOPPY("floppy"), UNRECOGNIZED("unrecognized"); public static final List<ImageType> ALL = ImmutableList.of(ISO, FLOPPY); protected final String stringValue; ImageType(String stringValue) { this.stringValue = stringValue; } public String value() { return stringValue; } protected static final Map<String, ImageType> STATUS_BY_ID = Maps.uniqueIndex( ImmutableSet.copyOf(ImageType.values()), new Function<ImageType, String>() { @Override public String apply(ImageType input) { return input.stringValue; } }); public static ImageType fromValue(String value) { ImageType type = STATUS_BY_ID.get(checkNotNull(value, "stringValue")); return type == null ? UNRECOGNIZED : type; } }
/** * 查询可供选择加入投放的宝贝 * * @param userId * @param activityId * @param onSale * @param query * @param sellerCids * @param pageNum * @param pageSize * @return * @throws TaobaoAccessControlException * @throws TaobaoEnhancedApiException * @throws TaobaoSessionExpiredException */ @NotNull public Page<PublishItem> paginateItems( Long userId, Long activityId, boolean onSale, String query, List<Long> sellerCids, int pageNum, int pageSize) throws TaobaoAccessControlException, TaobaoEnhancedApiException, TaobaoSessionExpiredException { Page<Item> itemPage; List<PublishItem> returnPublishItems = Lists.newArrayList(); // 从淘宝 api 获取商品 if (onSale) { itemPage = taobaoApiShopService.paginateOnSaleItems( userId, query, ITEM_FIELDS, sellerCids, pageNum, pageSize, true, null, "modified:asc"); } else { itemPage = taobaoApiShopService.paginateInventoryItems( userId, query, ITEM_FIELDS, Lists.newArrayList(BANNER_FOR_SHELVED), sellerCids, pageNum, pageSize, true, "modified:asc"); } List<Item> taobaoItems = itemPage.getItems(); if (CollectionUtils.isEmpty(taobaoItems)) { return Page.create( itemPage.getTotalSize(), pageNum, pageSize, Lists.<PublishItem>newArrayList()); } Map<Long, PosterRecommendPublishItem> indexMap = Maps.newHashMap(); if (null != activityId) { // 获取已经加入投放的宝贝并且设置是否加入投放的标志位 List<PosterRecommendPublishItem> posterRecommendPublishItems = posterRecommendPublishItemDAO.selectByUserIdAndActivityId(userId, activityId); indexMap = Maps.uniqueIndex( posterRecommendPublishItems, new Function<PosterRecommendPublishItem, Long>() { @Nullable @Override public Long apply(@Nullable PosterRecommendPublishItem input) { return input.getItemNumIid(); } }); } for (Item item : taobaoItems) { PublishItem publishItem = new PublishItem(); publishItem.setItem(item); if (indexMap.keySet().contains(item.getNumIid())) { publishItem.setPublishItemStatus(indexMap.get(item.getNumIid()).getStatus()); } else { publishItem.setPublishItemStatus(PUBLISH_ITEM_STATUS_NOT_IN_DB); } returnPublishItems.add(publishItem); } return Page.create(itemPage.getTotalSize(), pageNum, pageSize, returnPublishItems); }
public class NetconfMessageTransformer implements MessageTransformer<NetconfMessage> { public static final String MESSAGE_ID_PREFIX = "m"; private static final Logger LOG = LoggerFactory.getLogger(NetconfMessageTransformer.class); private static final Function<SchemaNode, QName> QNAME_FUNCTION = new Function<SchemaNode, QName>() { @Override public QName apply(final SchemaNode rpcDefinition) { return rpcDefinition.getQName(); } }; private static final Function<SchemaNode, QName> QNAME_NOREV_FUNCTION = new Function<SchemaNode, QName>() { @Override public QName apply(final SchemaNode notification) { return QNAME_FUNCTION.apply(notification).withoutRevision(); } }; private static final SchemaContext BASE_NETCONF_CTX; static { try { final ModuleInfoBackedContext moduleInfoBackedContext = ModuleInfoBackedContext.create(); moduleInfoBackedContext.addModuleInfos( Lists.newArrayList( org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601 .$YangModuleInfoImpl.getInstance())); BASE_NETCONF_CTX = moduleInfoBackedContext.tryToCreateSchemaContext().get(); } catch (final RuntimeException e) { LOG.error("Unable to prepare schema context for base netconf ops", e); throw new ExceptionInInitializerError(e); } } private static final Map<QName, RpcDefinition> MAPPED_BASE_RPCS = Maps.uniqueIndex(BASE_NETCONF_CTX.getOperations(), QNAME_FUNCTION); private final SchemaContext schemaContext; private final MessageCounter counter; private final Map<QName, RpcDefinition> mappedRpcs; private final Multimap<QName, NotificationDefinition> mappedNotifications; private final DomToNormalizedNodeParserFactory parserFactory; public NetconfMessageTransformer(final SchemaContext schemaContext, final boolean strictParsing) { this.counter = new MessageCounter(); this.schemaContext = schemaContext; parserFactory = DomToNormalizedNodeParserFactory.getInstance( XmlUtils.DEFAULT_XML_CODEC_PROVIDER, schemaContext, strictParsing); mappedRpcs = Maps.uniqueIndex(schemaContext.getOperations(), QNAME_FUNCTION); mappedNotifications = Multimaps.index(schemaContext.getNotifications(), QNAME_NOREV_FUNCTION); } @Override public synchronized DOMNotification toNotification(final NetconfMessage message) { final Map.Entry<Date, XmlElement> stripped = stripNotification(message); final QName notificationNoRev; try { notificationNoRev = QName.create(stripped.getValue().getNamespace(), stripped.getValue().getName()) .withoutRevision(); } catch (final MissingNameSpaceException e) { throw new IllegalArgumentException( "Unable to parse notification " + message + ", cannot find namespace", e); } final Collection<NotificationDefinition> notificationDefinitions = mappedNotifications.get(notificationNoRev); Preconditions.checkArgument( notificationDefinitions.size() > 0, "Unable to parse notification %s, unknown notification. Available notifications: %s", notificationDefinitions, mappedNotifications.keySet()); // FIXME if multiple revisions for same notifications are present, we should pick the most // recent. Or ? // We should probably just put the most recent notification versions into our map. We can expect // that the device sends the data according to the latest available revision of a model. final NotificationDefinition next = notificationDefinitions.iterator().next(); // We wrap the notification as a container node in order to reuse the parsers and builders for // container node final ContainerSchemaNode notificationAsContainerSchemaNode = NetconfMessageTransformUtil.createSchemaForNotification(next); final Element element = stripped.getValue().getDomElement(); final ContainerNode content; try { content = parserFactory .getContainerNodeParser() .parse(Collections.singleton(element), notificationAsContainerSchemaNode); } catch (IllegalArgumentException e) { throw new IllegalArgumentException( String.format("Failed to parse notification %s", element), e); } return new NetconfDeviceNotification(content, stripped.getKey()); } private static final ThreadLocal<SimpleDateFormat> EVENT_TIME_FORMAT = new ThreadLocal<SimpleDateFormat>() { @Override protected SimpleDateFormat initialValue() { final SimpleDateFormat withMillis = new SimpleDateFormat(NetconfNotification.RFC3339_DATE_FORMAT_WITH_MILLIS_BLUEPRINT); return new SimpleDateFormat(NetconfNotification.RFC3339_DATE_FORMAT_BLUEPRINT) { private static final long serialVersionUID = 1L; @Override public Date parse(final String source) throws ParseException { try { return super.parse(source); } catch (ParseException e) { // In case of failure, try to parse with milliseconds return withMillis.parse(source); } } }; } @Override public void set(final SimpleDateFormat value) { throw new UnsupportedOperationException(); } }; // FIXME move somewhere to util private static Map.Entry<Date, XmlElement> stripNotification(final NetconfMessage message) { final XmlElement xmlElement = XmlElement.fromDomDocument(message.getDocument()); final List<XmlElement> childElements = xmlElement.getChildElements(); Preconditions.checkArgument( childElements.size() == 2, "Unable to parse notification %s, unexpected format", message); final XmlElement eventTimeElement; final XmlElement notificationElement; if (childElements.get(0).getName().equals(EVENT_TIME)) { eventTimeElement = childElements.get(0); notificationElement = childElements.get(1); } else if (childElements.get(1).getName().equals(EVENT_TIME)) { eventTimeElement = childElements.get(1); notificationElement = childElements.get(0); } else { throw new IllegalArgumentException( "Notification payload does not contain " + EVENT_TIME + " " + message); } try { return new AbstractMap.SimpleEntry<>( EVENT_TIME_FORMAT.get().parse(eventTimeElement.getTextContent()), notificationElement); } catch (DocumentedException e) { throw new IllegalArgumentException( "Notification payload does not contain " + EVENT_TIME + " " + message); } catch (ParseException e) { LOG.warn( "Unable to parse event time from {}. Setting time to {}", eventTimeElement, NetconfNotification.UNKNOWN_EVENT_TIME, e); return new AbstractMap.SimpleEntry<>( NetconfNotification.UNKNOWN_EVENT_TIME, notificationElement); } } @Override public NetconfMessage toRpcRequest(SchemaPath rpc, final NormalizedNode<?, ?> payload) { // In case no input for rpc is defined, we can simply construct the payload here final QName rpcQName = rpc.getLastComponent(); Map<QName, RpcDefinition> currentMappedRpcs = mappedRpcs; // Determine whether a base netconf operation is being invoked and also check if the device // exposed model for base netconf // If no, use pre built base netconf operations model final boolean needToUseBaseCtx = mappedRpcs.get(rpcQName) == null && isBaseRpc(rpcQName); if (needToUseBaseCtx) { currentMappedRpcs = MAPPED_BASE_RPCS; } Preconditions.checkNotNull( currentMappedRpcs.get(rpcQName), "Unknown rpc %s, available rpcs: %s", rpcQName, currentMappedRpcs.keySet()); if (currentMappedRpcs.get(rpcQName).getInput() == null) { return new NetconfMessage( prepareDomResultForRpcRequest(rpcQName).getNode().getOwnerDocument()); } Preconditions.checkNotNull( payload, "Transforming an rpc with input: %s, payload cannot be null", rpcQName); Preconditions.checkArgument( payload instanceof ContainerNode, "Transforming an rpc with input: %s, payload has to be a container, but was: %s", rpcQName, payload); // Set the path to the input of rpc for the node stream writer rpc = rpc.createChild(QName.create(rpcQName, "input").intern()); final DOMResult result = prepareDomResultForRpcRequest(rpcQName); try { // If the schema context for netconf device does not contain model for base netconf // operations, use default pre build context with just the base model // This way operations like lock/unlock are supported even if the source for base model was // not provided writeNormalizedRpc( ((ContainerNode) payload), result, rpc, needToUseBaseCtx ? BASE_NETCONF_CTX : schemaContext); } catch (final XMLStreamException | IOException | IllegalStateException e) { throw new IllegalStateException("Unable to serialize " + rpc, e); } final Document node = result.getNode().getOwnerDocument(); return new NetconfMessage(node); } private static boolean isBaseRpc(final QName rpc) { return rpc.getNamespace().equals(NETCONF_URI); } private DOMResult prepareDomResultForRpcRequest(final QName rpcQName) { final Document document = XmlUtil.newDocument(); final Element rpcNS = document.createElementNS( NETCONF_RPC_QNAME.getNamespace().toString(), NETCONF_RPC_QNAME.getLocalName()); // set msg id rpcNS.setAttribute( NetconfMessageTransformUtil.MESSAGE_ID_ATTR, counter.getNewMessageId(MESSAGE_ID_PREFIX)); final Element elementNS = document.createElementNS(rpcQName.getNamespace().toString(), rpcQName.getLocalName()); rpcNS.appendChild(elementNS); document.appendChild(rpcNS); return new DOMResult(elementNS); } private static void writeNormalizedRpc( final ContainerNode normalized, final DOMResult result, final SchemaPath schemaPath, final SchemaContext baseNetconfCtx) throws IOException, XMLStreamException { final XMLStreamWriter writer = NetconfMessageTransformUtil.XML_FACTORY.createXMLStreamWriter(result); try { try (final NormalizedNodeStreamWriter normalizedNodeStreamWriter = XMLStreamNormalizedNodeStreamWriter.create(writer, baseNetconfCtx, schemaPath)) { try (final OrderedNormalizedNodeWriter normalizedNodeWriter = new OrderedNormalizedNodeWriter( normalizedNodeStreamWriter, baseNetconfCtx, schemaPath)) { Collection<DataContainerChild<?, ?>> value = normalized.getValue(); normalizedNodeWriter.write(value); normalizedNodeWriter.flush(); } } } finally { try { writer.close(); } catch (final Exception e) { LOG.warn("Unable to close resource properly", e); } } } @Override public synchronized DOMRpcResult toRpcResult(final NetconfMessage message, final SchemaPath rpc) { final NormalizedNode<?, ?> normalizedNode; final QName rpcQName = rpc.getLastComponent(); if (NetconfMessageTransformUtil.isDataRetrievalOperation(rpcQName)) { final Element xmlData = NetconfMessageTransformUtil.getDataSubtree(message.getDocument()); final ContainerSchemaNode schemaForDataRead = NetconfMessageTransformUtil.createSchemaForDataRead(schemaContext); final ContainerNode dataNode; try { dataNode = parserFactory .getContainerNodeParser() .parse(Collections.singleton(xmlData), schemaForDataRead); } catch (IllegalArgumentException e) { throw new IllegalArgumentException( String.format("Failed to parse data response %s", xmlData), e); } normalizedNode = Builders.containerBuilder() .withNodeIdentifier( new YangInstanceIdentifier.NodeIdentifier( NetconfMessageTransformUtil.NETCONF_RPC_REPLY_QNAME)) .withChild(dataNode) .build(); } else { Map<QName, RpcDefinition> currentMappedRpcs = mappedRpcs; // Determine whether a base netconf operation is being invoked and also check if the device // exposed model for base netconf // If no, use pre built base netconf operations model final boolean needToUseBaseCtx = mappedRpcs.get(rpcQName) == null && isBaseRpc(rpcQName); if (needToUseBaseCtx) { currentMappedRpcs = MAPPED_BASE_RPCS; } final RpcDefinition rpcDefinition = currentMappedRpcs.get(rpcQName); Preconditions.checkArgument( rpcDefinition != null, "Unable to parse response of %s, the rpc is unknown", rpcQName); // In case no input for rpc is defined, we can simply construct the payload here if (rpcDefinition.getOutput() == null) { Preconditions.checkArgument( XmlElement.fromDomDocument(message.getDocument()) .getOnlyChildElementWithSameNamespaceOptionally("ok") .isPresent(), "Unexpected content in response of rpc: %s, %s", rpcDefinition.getQName(), message); normalizedNode = null; } else { final Element element = message.getDocument().getDocumentElement(); try { normalizedNode = parserFactory .getContainerNodeParser() .parse(Collections.singleton(element), rpcDefinition.getOutput()); } catch (IllegalArgumentException e) { throw new IllegalArgumentException( String.format("Failed to parse RPC response %s", element), e); } } } return new DefaultDOMRpcResult(normalizedNode); } private static class NetconfDeviceNotification implements DOMNotification, DOMEvent { private final ContainerNode content; private final SchemaPath schemaPath; private final Date eventTime; NetconfDeviceNotification(final ContainerNode content, final Date eventTime) { this.content = content; this.eventTime = eventTime; this.schemaPath = toPath(content.getNodeType()); } @Nonnull @Override public SchemaPath getType() { return schemaPath; } @Nonnull @Override public ContainerNode getBody() { return content; } @Override public Date getEventTime() { return eventTime; } } }