// TODO move this kind of logic into AttributeDefinition itself private static ModelNode validateResolvedModel( final AttributeDefinition definition, final OperationContext context, final ModelNode subModel) throws OperationFailedException { final String attributeName = definition.getName(); final boolean has = subModel.has(attributeName); if (!has && definition.isRequired(subModel)) { throw ServerMessages.MESSAGES.attributeIsRequired(attributeName); } ModelNode result; if (has) { if (!definition.isAllowed(subModel)) { if (subModel.hasDefined(attributeName)) { throw ServerMessages.MESSAGES.attributeNotAllowedWhenAlternativeIsPresent( attributeName, Arrays.asList(definition.getAlternatives())); } else { // create the undefined node result = new ModelNode(); } } else { result = definition.resolveModelAttribute(context, subModel); } } else { // create the undefined node result = new ModelNode(); } return result; }
private static void validateDirectory(String property, File file) { if (!file.exists()) { throw ServerMessages.MESSAGES.propertySpecifiedFileDoesNotExist( property, file.getAbsolutePath()); } if (!file.isDirectory()) { throw ServerMessages.MESSAGES.propertySpecifiedFileIsNotADirectory( property, file.getAbsolutePath()); } }
/** {@inheritDoc} */ public ClassSpec getClassSpec(final String name) throws IOException { final VirtualFile file = root.getChild(name); if (!file.exists()) { return null; } final long size = file.getSize(); final ClassSpec spec = new ClassSpec(); final InputStream is = file.openStream(); try { if (size <= (long) Integer.MAX_VALUE) { final int castSize = (int) size; byte[] bytes = new byte[castSize]; int a = 0, res; while ((res = is.read(bytes, a, castSize - a)) > 0) { a += res; } // done is.close(); spec.setBytes(bytes); spec.setCodeSource(new CodeSource(rootUrl, file.getCodeSigners())); return spec; } else { throw ServerMessages.MESSAGES.resourceTooLarge(); } } finally { VFSUtils.safeClose(is); } }
private static void copyDirectory(File src, File dest) { for (String current : src.list()) { final File srcFile = new File(src, current); final File destFile = new File(dest, current); if (srcFile.isDirectory()) { destFile.mkdir(); copyDirectory(srcFile, destFile); } else { try { final InputStream in = new BufferedInputStream(new FileInputStream(srcFile)); final OutputStream out = new BufferedOutputStream(new FileOutputStream(destFile)); try { int i; while ((i = in.read()) != -1) { out.write(i); } } catch (IOException e) { throw ServerMessages.MESSAGES.errorCopyingFile( srcFile.getAbsolutePath(), destFile.getAbsolutePath(), e); } finally { StreamUtils.safeClose(in); StreamUtils.safeClose(out); } } catch (FileNotFoundException e) { throw MESSAGES.cannotSetupEmbeddedServer(e); } } } }
private static File getFileUnderAsRoot( File jbossHomeDir, Properties props, String propName, String relativeLocation, boolean mustExist) { String prop = props.getProperty(propName, null); if (prop == null) { prop = props.getProperty(ServerEnvironment.SERVER_BASE_DIR, null); if (prop == null) { File dir = new File(jbossHomeDir, "standalone" + File.separator + relativeLocation); if (mustExist && (!dir.exists() || !dir.isDirectory())) { throw ServerMessages.MESSAGES.embeddedServerDirectoryNotFound( "standalone" + File.separator + relativeLocation, jbossHomeDir.getAbsolutePath()); } return dir; } else { File server = new File(prop); validateDirectory(ServerEnvironment.SERVER_BASE_DIR, server); return new File(server, relativeLocation); } } else { File dir = new File(prop); validateDirectory(ServerEnvironment.SERVER_CONFIG_DIR, dir); return dir; } }
// TODO move this kind of logic into AttributeDefinition itself private static void validateAndSet( final AttributeDefinition definition, final ModelNode operation, final ModelNode subModel) throws OperationFailedException { final String attributeName = definition.getName(); final boolean has = operation.has(attributeName); if (!has && definition.isRequired(operation)) { throw ServerMessages.MESSAGES.attributeIsRequired(attributeName); } if (has) { if (!definition.isAllowed(operation)) { throw ServerMessages.MESSAGES.attributeIsInvalid(attributeName); } definition.validateAndSet(operation, subModel); } else { // create the undefined node subModel.get(definition.getName()); } }
/** {@inheritDoc} */ @Override protected InputStream getContentInputStream( OperationContext operationContext, ModelNode operation) throws IOException, OperationFailedException { // Validate the operation streamValidator.validate(operation); // Get the attached stream final int streamIndex = operation.require(INPUT_STREAM_INDEX).asInt(); final InputStream in = operationContext.getAttachmentStream(streamIndex); if (in == null) { throw ServerMessages.MESSAGES.nullStreamAttachment(streamIndex); } return in; }
@Override public Closeable mountDeploymentContent( final VirtualFile contents, VirtualFile mountPoint, MountType type) throws IOException { // according to the javadoc contents can not be null assert contents != null : "null contents"; switch (type) { case ZIP: return VFS.mountZip(contents, mountPoint, tempFileProvider); case EXPANDED: return VFS.mountZipExpanded(contents, mountPoint, tempFileProvider); case REAL: return VFS.mountReal(contents.getPhysicalFile(), mountPoint); default: throw ServerMessages.MESSAGES.unknownMountType(type); } }
@Override public void start(StartContext context) throws StartException { try { final JBossThreadFactory threadFactory = new JBossThreadFactory( new ThreadGroup("ServerDeploymentRepository-temp-threads"), Boolean.FALSE, null, "%G - %t", null, null, doPrivileged(GetAccessControlContextAction.getInstance())); tempFileProvider = TempFileProvider.create("temp", Executors.newScheduledThreadPool(2, threadFactory)); } catch (IOException e) { throw ServerMessages.MESSAGES.failedCreatingTempProvider(); } ServerLogger.ROOT_LOGGER.debugf( "%s started", DeploymentMountProvider.class.getSimpleName()); }
/** * Replace properties of the form: <code> * ${<i><[env.]name>[</i>,<i><[env.]name2>[</i>,<i><[env.]name3>...]][</i>:<i><default>]</i>} * </code> * * @param value - either a system property or environment variable reference * @return the value of the system property or environment variable referenced if it exists */ public static String replaceProperties(final String value) { final StringBuilder builder = new StringBuilder(); final int len = value.length(); int state = INITIAL; int start = -1; int nameStart = -1; String resolvedValue = null; for (int i = 0; i < len; i = value.offsetByCodePoints(i, 1)) { final int ch = value.codePointAt(i); switch (state) { case INITIAL: { switch (ch) { case '$': { state = GOT_DOLLAR; continue; } default: { builder.appendCodePoint(ch); continue; } } // not reachable } case GOT_DOLLAR: { switch (ch) { case '$': { builder.appendCodePoint(ch); state = INITIAL; continue; } case '{': { start = i + 1; nameStart = start; state = GOT_OPEN_BRACE; continue; } default: { // invalid; emit and resume builder.append('$').appendCodePoint(ch); state = INITIAL; continue; } } // not reachable } case GOT_OPEN_BRACE: { switch (ch) { case ':': case '}': case ',': { final String name = value.substring(nameStart, i).trim(); if ("/".equals(name)) { builder.append(File.separator); state = ch == '}' ? INITIAL : RESOLVED; continue; } else if (":".equals(name)) { builder.append(File.pathSeparator); state = ch == '}' ? INITIAL : RESOLVED; continue; } // First check for system property, then env variable String val = System.getProperty(name); if (val == null && name.startsWith("env.")) val = System.getenv(name.substring(4)); if (val != null) { builder.append(val); resolvedValue = val; state = ch == '}' ? INITIAL : RESOLVED; continue; } else if (ch == ',') { nameStart = i + 1; continue; } else if (ch == ':') { start = i + 1; state = DEFAULT; continue; } else { throw ServerMessages.MESSAGES.failedToResolveExpression( value.substring(start - 2, i + 1)); } } default: { continue; } } // not reachable } case RESOLVED: { if (ch == '}') { state = INITIAL; } continue; } case DEFAULT: { if (ch == '}') { state = INITIAL; builder.append(value.substring(start, i)); } continue; } default: throw ServerMessages.MESSAGES.unexpectedChar("" + ch); } } switch (state) { case GOT_DOLLAR: { builder.append('$'); break; } case DEFAULT: { builder.append(value.substring(start - 2)); break; } case GOT_OPEN_BRACE: { // We had a reference that was not resolved, throw ISE if (resolvedValue == null) throw ServerMessages.MESSAGES.incompleteExpression(builder.toString()); break; } } return builder.toString(); }