/** * @param springCfgPath Spring configuration file path. * @return Grid configuration. * @throws Exception If failed. */ protected static IgniteConfiguration loadConfiguration(String springCfgPath) throws Exception { URL url; try { url = new URL(springCfgPath); } catch (MalformedURLException e) { url = IgniteUtils.resolveIgniteUrl(springCfgPath); if (url == null) throw new IgniteCheckedException( "Spring XML configuration path is invalid: " + springCfgPath + ". Note that this path should be either absolute or a relative local file system path, " + "relative to META-INF in classpath or valid URL to IGNITE_HOME.", e); } GenericApplicationContext springCtx; try { springCtx = new GenericApplicationContext(); new XmlBeanDefinitionReader(springCtx).loadBeanDefinitions(new UrlResource(url)); springCtx.refresh(); } catch (BeansException e) { throw new Exception( "Failed to instantiate Spring XML application context [springUrl=" + url + ", err=" + e.getMessage() + ']', e); } Map<String, IgniteConfiguration> cfgMap; try { cfgMap = springCtx.getBeansOfType(IgniteConfiguration.class); } catch (BeansException e) { throw new Exception( "Failed to instantiate bean [type=" + IgniteConfiguration.class + ", err=" + e.getMessage() + ']', e); } if (cfgMap == null || cfgMap.isEmpty()) throw new Exception("Failed to find ignite configuration in: " + url); return cfgMap.values().iterator().next(); }
protected Object[] getArgumentsForConstraint( String objectName, String field, ConstraintDescriptor<?> descriptor) { List<Object> arguments = new LinkedList<>(); String[] codes = new String[] {objectName + Errors.NESTED_PATH_SEPARATOR + field, field}; arguments.add(new DefaultMessageSourceResolvable(codes, field)); // Using a TreeMap for alphabetical ordering of attribute names Map<String, Object> attributesToExpose = new TreeMap<>(); for (Map.Entry<String, Object> entry : descriptor.getAttributes().entrySet()) { String attributeName = entry.getKey(); Object attributeValue = entry.getValue(); if (!internalAnnotationAttributes.contains(attributeName)) { attributesToExpose.put(attributeName, attributeValue); } } arguments.addAll(attributesToExpose.values()); return arguments.toArray(new Object[arguments.size()]); }
/** * Starts new grid node. * * @param gridName name of new node. * @param springCfg file with spring configuration to use for this node. * @return a grid instance local to new node {@link GridGain#start(GridConfiguration)}. * @throws Exception if node run failed. */ protected Grid startNode(String gridName, File springCfg) throws Exception { assert springCfg != null; ListableBeanFactory springCtx = new FileSystemXmlApplicationContext("file:///" + springCfg.getAbsolutePath()); Map cfgMap = springCtx.getBeansOfType(GridConfiguration.class); assert cfgMap != null; assert !cfgMap.isEmpty(); GridConfiguration cfg = (GridConfiguration) cfgMap.values().iterator().next(); cfg.setGridName(gridName + "-" + getNextNodeNum()); return G.start(cfg); }
/** * Binds request data to this form - that is, handles form submission. * * @return a copy of this form filled with the new data */ public Form<T> bindFromRequest(Map<String, String[]> requestData, String... allowedFields) { Map<String, String> data = new HashMap<>(); for (String key : requestData.keySet()) { String[] values = requestData.get(key); if (key.endsWith("[]")) { String k = key.substring(0, key.length() - 2); for (int i = 0; i < values.length; i++) { data.put(k + "[" + i + "]", values[i]); } } else { if (values.length > 0) { data.put(key, values[0]); } } } return bind(data, allowedFields); }
/** * Load grid configuration for specified node type. * * @param type Node type to load configuration for. * @return Grid configuration for specified node type. */ static IgniteConfiguration getConfig(String type) { String path = NODE_CFG.get(type); if (path == null) throw new IllegalArgumentException("Unsupported node type: " + type); URL url = U.resolveIgniteUrl(path); BeanFactory ctx = new FileSystemXmlApplicationContext(url.toString()); return (IgniteConfiguration) ctx.getBean("grid.cfg"); }
/** * Example for start/stop node tasks. * * @param args Not used. */ public static void main(String[] args) { String nodeType = "tcp+ssl"; // Start initial node = 1 try (Ignite g = G.start(NODE_CFG.get(nodeType))) { // Change topology. changeTopology(g, 4, 1, nodeType); changeTopology(g, 1, 4, nodeType); // Stop node by id = 0 g.compute().execute(ClientStopNodeTask.class, g.cluster().localNode().id().toString()); // Wait for node stops. // U.sleep(1000); assert G.allGrids().isEmpty(); } catch (Exception e) { System.err.println("Uncaught exception: " + e.getMessage()); e.printStackTrace(System.err); } }
/** * Retrieve a field. * * @param key field name * @return the field (even if the field does not exist you get a field) */ public Field field(String key) { // Value String fieldValue = null; if (data.containsKey(key)) { fieldValue = data.get(key); } else { if (value.isPresent()) { BeanWrapper beanWrapper = new BeanWrapperImpl(value.get()); beanWrapper.setAutoGrowNestedPaths(true); String objectKey = key; if (rootName != null && key.startsWith(rootName + ".")) { objectKey = key.substring(rootName.length() + 1); } if (beanWrapper.isReadableProperty(objectKey)) { Object oValue = beanWrapper.getPropertyValue(objectKey); if (oValue != null) { final String objectKeyFinal = objectKey; fieldValue = withRequestLocale( () -> formatters.print( beanWrapper.getPropertyTypeDescriptor(objectKeyFinal), oValue)); } } } } // Error List<ValidationError> fieldErrors = errors.get(key); if (fieldErrors == null) { fieldErrors = new ArrayList<>(); } // Format Tuple<String, List<Object>> format = null; BeanWrapper beanWrapper = new BeanWrapperImpl(blankInstance()); beanWrapper.setAutoGrowNestedPaths(true); try { for (Annotation a : beanWrapper.getPropertyTypeDescriptor(key).getAnnotations()) { Class<?> annotationType = a.annotationType(); if (annotationType.isAnnotationPresent(play.data.Form.Display.class)) { play.data.Form.Display d = annotationType.getAnnotation(play.data.Form.Display.class); if (d.name().startsWith("format.")) { List<Object> attributes = new ArrayList<>(); for (String attr : d.attributes()) { Object attrValue = null; try { attrValue = a.getClass().getDeclaredMethod(attr).invoke(a); } catch (Exception e) { // do nothing } attributes.add(attrValue); } format = Tuple(d.name(), attributes); } } } } catch (NullPointerException e) { // do nothing } // Constraints List<Tuple<String, List<Object>>> constraints = new ArrayList<>(); Class<?> classType = backedType; String leafKey = key; if (rootName != null && leafKey.startsWith(rootName + ".")) { leafKey = leafKey.substring(rootName.length() + 1); } int p = leafKey.lastIndexOf('.'); if (p > 0) { classType = beanWrapper.getPropertyType(leafKey.substring(0, p)); leafKey = leafKey.substring(p + 1); } if (classType != null) { BeanDescriptor beanDescriptor = play.data.validation.Validation.getValidator().getConstraintsForClass(classType); if (beanDescriptor != null) { PropertyDescriptor property = beanDescriptor.getConstraintsForProperty(leafKey); if (property != null) { constraints = Constraints.displayableConstraint(property.getConstraintDescriptors()); } } } return new Field(this, key, constraints, format, fieldErrors, fieldValue); }
/** * Binds data to this form - that is, handles form submission. * * @param data data to submit * @return a copy of this form filled with the new data */ @SuppressWarnings("unchecked") public Form<T> bind(Map<String, String> data, String... allowedFields) { DataBinder dataBinder; Map<String, String> objectData = data; if (rootName == null) { dataBinder = new DataBinder(blankInstance()); } else { dataBinder = new DataBinder(blankInstance(), rootName); objectData = new HashMap<>(); for (String key : data.keySet()) { if (key.startsWith(rootName + ".")) { objectData.put(key.substring(rootName.length() + 1), data.get(key)); } } } if (allowedFields.length > 0) { dataBinder.setAllowedFields(allowedFields); } SpringValidatorAdapter validator = new SpringValidatorAdapter(play.data.validation.Validation.getValidator()); dataBinder.setValidator(validator); dataBinder.setConversionService(formatters.conversion); dataBinder.setAutoGrowNestedPaths(true); final Map<String, String> objectDataFinal = objectData; withRequestLocale( () -> { dataBinder.bind(new MutablePropertyValues(objectDataFinal)); return null; }); Set<ConstraintViolation<Object>> validationErrors; if (groups != null) { validationErrors = validator.validate(dataBinder.getTarget(), groups); } else { validationErrors = validator.validate(dataBinder.getTarget()); } BindingResult result = dataBinder.getBindingResult(); for (ConstraintViolation<Object> violation : validationErrors) { String field = violation.getPropertyPath().toString(); FieldError fieldError = result.getFieldError(field); if (fieldError == null || !fieldError.isBindingFailure()) { try { result.rejectValue( field, violation.getConstraintDescriptor().getAnnotation().annotationType().getSimpleName(), getArgumentsForConstraint( result.getObjectName(), field, violation.getConstraintDescriptor()), getMessageForConstraintViolation(violation)); } catch (NotReadablePropertyException ex) { throw new IllegalStateException( "JSR-303 validated property '" + field + "' does not have a corresponding accessor for data binding - " + "check your DataBinder's configuration (bean property versus direct field access)", ex); } } } if (result.hasErrors() || result.getGlobalErrorCount() > 0) { Map<String, List<ValidationError>> errors = new HashMap<>(); for (FieldError error : result.getFieldErrors()) { String key = error.getObjectName() + "." + error.getField(); if (key.startsWith("target.") && rootName == null) { key = key.substring(7); } if (!errors.containsKey(key)) { errors.put(key, new ArrayList<>()); } ValidationError validationError; if (error.isBindingFailure()) { ImmutableList.Builder<String> builder = ImmutableList.builder(); Optional<Messages> msgs = Optional.of(Http.Context.current.get()).map(c -> messagesApi.preferred(c.request())); for (String code : error.getCodes()) { code = code.replace("typeMismatch", "error.invalid"); if (!msgs.isPresent() || msgs.get().isDefinedAt(code)) { builder.add(code); } } validationError = new ValidationError( key, builder.build().reverse(), convertErrorArguments(error.getArguments())); } else { validationError = new ValidationError( key, error.getDefaultMessage(), convertErrorArguments(error.getArguments())); } errors.get(key).add(validationError); } List<ValidationError> globalErrors = new ArrayList<>(); for (ObjectError error : result.getGlobalErrors()) { globalErrors.add( new ValidationError( "", error.getDefaultMessage(), convertErrorArguments(error.getArguments()))); } if (!globalErrors.isEmpty()) { errors.put("", globalErrors); } return new Form( rootName, backedType, data, errors, Optional.empty(), groups, messagesApi, formatters); } else { Object globalError = null; if (result.getTarget() != null) { try { java.lang.reflect.Method v = result.getTarget().getClass().getMethod("validate"); globalError = v.invoke(result.getTarget()); } catch (NoSuchMethodException e) { // do nothing } catch (Throwable e) { throw new RuntimeException(e); } } if (globalError != null) { Map<String, List<ValidationError>> errors = new HashMap<>(); if (globalError instanceof String) { errors.put("", new ArrayList<>()); errors.get("").add(new ValidationError("", (String) globalError, new ArrayList())); } else if (globalError instanceof List) { for (ValidationError error : (List<ValidationError>) globalError) { List<ValidationError> errorsForKey = errors.get(error.key()); if (errorsForKey == null) { errors.put(error.key(), errorsForKey = new ArrayList<>()); } errorsForKey.add(error); } } else if (globalError instanceof Map) { errors = (Map<String, List<ValidationError>>) globalError; } return new Form( rootName, backedType, data, errors, Optional.empty(), groups, messagesApi, formatters); } return new Form( rootName, backedType, new HashMap<>(data), new HashMap<>(errors), Optional.ofNullable((T) result.getTarget()), groups, messagesApi, formatters); } }
protected Map<String, String> requestData(Http.Request request) { Map<String, String[]> urlFormEncoded = new HashMap<>(); if (request.body().asFormUrlEncoded() != null) { urlFormEncoded = request.body().asFormUrlEncoded(); } Map<String, String[]> multipartFormData = new HashMap<>(); if (request.body().asMultipartFormData() != null) { multipartFormData = request.body().asMultipartFormData().asFormUrlEncoded(); } Map<String, String> jsonData = new HashMap<>(); if (request.body().asJson() != null) { jsonData = play.libs.Scala.asJava( play.api.data.FormUtils.fromJson( "", play.api.libs.json.Json.parse( play.libs.Json.stringify(request.body().asJson())))); } Map<String, String[]> queryString = request.queryString(); Map<String, String> data = new HashMap<>(); for (String key : urlFormEncoded.keySet()) { String[] values = urlFormEncoded.get(key); if (key.endsWith("[]")) { String k = key.substring(0, key.length() - 2); for (int i = 0; i < values.length; i++) { data.put(k + "[" + i + "]", values[i]); } } else { if (values.length > 0) { data.put(key, values[0]); } } } for (String key : multipartFormData.keySet()) { String[] values = multipartFormData.get(key); if (key.endsWith("[]")) { String k = key.substring(0, key.length() - 2); for (int i = 0; i < values.length; i++) { data.put(k + "[" + i + "]", values[i]); } } else { if (values.length > 0) { data.put(key, values[0]); } } } for (String key : jsonData.keySet()) { data.put(key, jsonData.get(key)); } for (String key : queryString.keySet()) { String[] values = queryString.get(key); if (key.endsWith("[]")) { String k = key.substring(0, key.length() - 2); for (int i = 0; i < values.length; i++) { data.put(k + "[" + i + "]", values[i]); } } else { if (values.length > 0) { data.put(key, values[0]); } } } return data; }
/** * See <a href="http://e-docs.bea.com/wls/docs100/javadocs/weblogic/common/T3StartupDef.html"> * http://e-docs.bea.com/wls/docs100/javadocs/weblogic/common/T3StartupDef.html</a> for more * information. * * @param str Virtual name by which the class is registered as a {@code startupClass} in the * {@code config.xml} file * @param params A hashtable that is made up of the name-value pairs supplied from the {@code * startupArgs} property * @return Result string (log message). * @throws Exception Thrown if error occurred. */ @SuppressWarnings({"unchecked", "CatchGenericClass"}) @Override public String startup(String str, Hashtable params) throws Exception { GridLogger log = new GridJavaLogger(LoggingHelper.getServerLogger()); cfgFile = (String) params.get(cfgFilePathParam); if (cfgFile == null) { throw new IllegalArgumentException("Failed to read property: " + cfgFilePathParam); } String workMgrName = (String) params.get(workMgrParam); URL cfgUrl = U.resolveGridGainUrl(cfgFile); if (cfgUrl == null) throw new ServerLifecycleException( "Failed to find Spring configuration file (path provided should be " + "either absolute, relative to GRIDGAIN_HOME, or relative to META-INF folder): " + cfgFile); GenericApplicationContext springCtx; try { springCtx = new GenericApplicationContext(); XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(springCtx); xmlReader.loadBeanDefinitions(new UrlResource(cfgUrl)); springCtx.refresh(); } catch (BeansException e) { throw new ServerLifecycleException( "Failed to instantiate Spring XML application context: " + e.getMessage(), e); } Map cfgMap; try { // Note: Spring is not generics-friendly. cfgMap = springCtx.getBeansOfType(GridConfiguration.class); } catch (BeansException e) { throw new ServerLifecycleException( "Failed to instantiate bean [type=" + GridConfiguration.class + ", err=" + e.getMessage() + ']', e); } if (cfgMap == null) throw new ServerLifecycleException( "Failed to find a single grid factory configuration in: " + cfgUrl); if (cfgMap.isEmpty()) throw new ServerLifecycleException("Can't find grid factory configuration in: " + cfgUrl); try { ExecutorService execSvc = null; MBeanServer mbeanSrv = null; for (GridConfiguration cfg : (Collection<GridConfiguration>) cfgMap.values()) { assert cfg != null; GridConfigurationAdapter adapter = new GridConfigurationAdapter(cfg); // Set logger. if (cfg.getGridLogger() == null) adapter.setGridLogger(log); if (cfg.getExecutorService() == null) { if (execSvc == null) execSvc = workMgrName != null ? new GridThreadWorkManagerExecutor(workMgrName) : new GridThreadWorkManagerExecutor(J2EEWorkManager.getDefault()); adapter.setExecutorService(execSvc); } if (cfg.getMBeanServer() == null) { if (mbeanSrv == null) { InitialContext ctx = null; try { ctx = new InitialContext(); mbeanSrv = (MBeanServer) ctx.lookup("java:comp/jmx/runtime"); } catch (Exception e) { throw new IllegalArgumentException( "MBean server was not provided and failed to obtain " + "Weblogic MBean server.", e); } finally { if (ctx != null) ctx.close(); } } adapter.setMBeanServer(mbeanSrv); } Grid grid = G.start(adapter, springCtx); // Test if grid is not null - started properly. if (grid != null) gridNames.add(grid.name()); } return getClass().getSimpleName() + " started successfully."; } catch (GridException e) { // Stop started grids only. for (String name : gridNames) G.stop(name, true); throw new ServerLifecycleException("Failed to start GridGain.", e); } }
/** * Binds data to this form - that is, handles form submission. * * @param data data to submit * @return a copy of this form filled with the new data */ public Form<T> bind(Map<String, String> data, String... allowedFields) { DataBinder dataBinder = null; Map<String, String> objectData = data; if (rootName == null) { dataBinder = new DataBinder(blankInstance()); } else { dataBinder = new DataBinder(blankInstance(), rootName); objectData = new HashMap<String, String>(); for (String key : data.keySet()) { if (key.startsWith(rootName + ".")) { objectData.put(key.substring(rootName.length() + 1), data.get(key)); } } } if (allowedFields.length > 0) { dataBinder.setAllowedFields(allowedFields); } SpringValidatorAdapter validator = new SpringValidatorAdapter(play.data.validation.Validation.getValidator()); dataBinder.setValidator(validator); dataBinder.setConversionService(play.data.format.Formatters.conversion); dataBinder.setAutoGrowNestedPaths(true); dataBinder.bind(new MutablePropertyValues(objectData)); Set<ConstraintViolation<Object>> validationErrors; if (groups != null) { validationErrors = validator.validate(dataBinder.getTarget(), groups); } else { validationErrors = validator.validate(dataBinder.getTarget()); } BindingResult result = dataBinder.getBindingResult(); for (ConstraintViolation<Object> violation : validationErrors) { String field = violation.getPropertyPath().toString(); FieldError fieldError = result.getFieldError(field); if (fieldError == null || !fieldError.isBindingFailure()) { try { result.rejectValue( field, violation.getConstraintDescriptor().getAnnotation().annotationType().getSimpleName(), getArgumentsForConstraint( result.getObjectName(), field, violation.getConstraintDescriptor()), violation.getMessage()); } catch (NotReadablePropertyException ex) { throw new IllegalStateException( "JSR-303 validated property '" + field + "' does not have a corresponding accessor for data binding - " + "check your DataBinder's configuration (bean property versus direct field access)", ex); } } } if (result.hasErrors()) { Map<String, List<ValidationError>> errors = new HashMap<String, List<ValidationError>>(); for (FieldError error : result.getFieldErrors()) { String key = error.getObjectName() + "." + error.getField(); if (key.startsWith("target.") && rootName == null) { key = key.substring(7); } List<Object> arguments = new ArrayList<Object>(); for (Object arg : error.getArguments()) { if (!(arg instanceof org.springframework.context.support.DefaultMessageSourceResolvable)) { arguments.add(arg); } } if (!errors.containsKey(key)) { errors.put(key, new ArrayList<ValidationError>()); } errors .get(key) .add( new ValidationError( key, error.isBindingFailure() ? "error.invalid" : error.getDefaultMessage(), arguments)); } return new Form(rootName, backedType, data, errors, None(), groups); } else { Object globalError = null; if (result.getTarget() != null) { try { java.lang.reflect.Method v = result.getTarget().getClass().getMethod("validate"); globalError = v.invoke(result.getTarget()); } catch (NoSuchMethodException e) { } catch (Throwable e) { throw new RuntimeException(e); } } if (globalError != null) { Map<String, List<ValidationError>> errors = new HashMap<String, List<ValidationError>>(); if (globalError instanceof String) { errors.put("", new ArrayList<ValidationError>()); errors.get("").add(new ValidationError("", (String) globalError, new ArrayList())); } else if (globalError instanceof List) { errors.put("", (List<ValidationError>) globalError); } else if (globalError instanceof Map) { errors = (Map<String, List<ValidationError>>) globalError; } return new Form(rootName, backedType, data, errors, None(), groups); } return new Form( rootName, backedType, new HashMap<String, String>(data), new HashMap<String, List<ValidationError>>(errors), Some((T) result.getTarget()), groups); } }