예제 #1
0
 /**
  * Returns a schema, given the name of a schema.
  *
  * @param schemaName a schema name.
  * @return a schema.
  */
 public static Schema getSchemaFromName(String schemaName) {
   SchemaManager schemaManager = Framework.getLocalService(SchemaManager.class);
   return schemaManager.getSchema(schemaName);
 }
  /** Recomputes all the info needed for efficient access. */
  private void recomputeSourceInfos() throws DirectoryException {

    final Schema schema = schemaManager.getSchema(schemaName);
    if (schema == null) {
      throw new DirectoryException(
          String.format("Directory '%s' has unknown schema '%s'", directory.getName(), schemaName));
    }
    final Set<String> sourceFields = new HashSet<String>();
    for (Field f : schema.getFields()) {
      sourceFields.add(f.getName().getLocalName());
    }
    if (!sourceFields.contains(schemaIdField)) {
      throw new DirectoryException(
          String.format(
              "Directory '%s' schema '%s' has no id field '%s'",
              directory.getName(), schemaName, schemaIdField));
    }

    List<SourceInfo> newSourceInfos = new ArrayList<SourceInfo>(2);
    for (SourceDescriptor source : descriptor.sources) {
      int ndirs = source.subDirectories.length;
      if (ndirs == 0) {
        throw new DirectoryException(
            String.format(
                "Directory '%s' source '%s' has no subdirectories",
                directory.getName(), source.name));
      }

      final List<SubDirectoryInfo> subDirectoryInfos = new ArrayList<SubDirectoryInfo>(ndirs);

      SubDirectoryInfo authDirectoryInfo = null;
      boolean hasRequiredDir = false;
      for (SubDirectoryDescriptor subDir : source.subDirectories) {
        final String dirName = subDir.name;
        final String dirSchemaName = directoryService.getDirectorySchema(dirName);
        final String dirIdField = directoryService.getDirectoryIdField(dirName);
        final boolean dirIsAuth = directoryService.getDirectoryPasswordField(dirName) != null;
        final Map<String, String> fromSource = new HashMap<String, String>();
        final Map<String, String> toSource = new HashMap<String, String>();
        final Map<String, Serializable> defaultEntry = new HashMap<String, Serializable>();
        final boolean dirIsOptional = subDir.isOptional;

        // XXX check authenticating
        final Schema dirSchema = schemaManager.getSchema(dirSchemaName);
        if (dirSchema == null) {
          throw new DirectoryException(
              String.format(
                  "Directory '%s' source '%s' subdirectory '%s' " + "has unknown schema '%s'",
                  directory.getName(), source.name, dirName, dirSchemaName));
        }
        // record default field mappings if same name and record default
        // values
        final Set<String> dirSchemaFields = new HashSet<String>();
        for (Field f : dirSchema.getFields()) {
          final String fieldName = f.getName().getLocalName();
          dirSchemaFields.add(fieldName);
          if (sourceFields.contains(fieldName)) {
            // XXX check no duplicates!
            fromSource.put(fieldName, fieldName);
            toSource.put(fieldName, fieldName);
          }
          // XXX cast to Serializable
          defaultEntry.put(fieldName, (Serializable) f.getDefaultValue());
        }
        // treat renamings
        // XXX id field ?
        for (FieldDescriptor field : subDir.fields) {
          final String sourceFieldName = field.forField;
          final String fieldName = field.name;
          if (!sourceFields.contains(sourceFieldName)) {
            throw new DirectoryException(
                String.format(
                    "Directory '%s' source '%s' subdirectory '%s' "
                        + "has mapping for unknown field '%s'",
                    directory.getName(), source.name, dirName, sourceFieldName));
          }
          if (!dirSchemaFields.contains(fieldName)) {
            throw new DirectoryException(
                String.format(
                    "Directory '%s' source '%s' subdirectory '%s' "
                        + "has mapping of unknown field' '%s'",
                    directory.getName(), source.name, dirName, fieldName));
          }
          fromSource.put(sourceFieldName, fieldName);
          toSource.put(fieldName, sourceFieldName);
        }
        SubDirectoryInfo subDirectoryInfo =
            new SubDirectoryInfo(
                dirName,
                dirSchemaName,
                dirIdField,
                dirIsAuth,
                fromSource,
                toSource,
                defaultEntry,
                dirIsOptional);
        subDirectoryInfos.add(subDirectoryInfo);

        if (dirIsAuth) {
          if (authDirectoryInfo != null) {
            throw new DirectoryException(
                String.format(
                    "Directory '%s' source '%s' has two subdirectories "
                        + "with a password field, '%s' and '%s'",
                    directory.getName(), source.name, authDirectoryInfo.dirName, dirName));
          }
          authDirectoryInfo = subDirectoryInfo;
        }
        if (!dirIsOptional) {
          hasRequiredDir = true;
        }
      }
      if (isAuthenticating() && authDirectoryInfo == null) {
        throw new DirectoryException(
            String.format(
                "Directory '%s' source '%s' has no subdirectory " + "with a password field",
                directory.getName(), source.name));
      }
      if (!hasRequiredDir) {
        throw new DirectoryException(
            String.format(
                "Directory '%s' source '%s' only has optional subdirectories: "
                    + "no directory can be used has a reference.",
                directory.getName(), source.name));
      }
      newSourceInfos.add(new SourceInfo(source, subDirectoryInfos, authDirectoryInfo));
    }
    sourceInfos = newSourceInfos;
  }