Permalink
Browse files

Added new built-in procedure "System.Type.List".

  • Loading branch information...
1 parent 1262297 commit 2722d31fe3953189ab91ad5be50b2828ea4f88fd @cederberg committed Apr 8, 2012
@@ -45,6 +45,7 @@
import org.rapidcontext.app.proc.ThreadCreateProcedure;
import org.rapidcontext.app.proc.ThreadInterruptProcedure;
import org.rapidcontext.app.proc.ThreadListProcedure;
+import org.rapidcontext.app.proc.TypeListProcedure;
import org.rapidcontext.app.proc.UserChangeProcedure;
import org.rapidcontext.app.proc.UserCheckAccessProcedure;
import org.rapidcontext.app.proc.UserListProcedure;
@@ -269,6 +270,7 @@ private void initLibrary() {
library.addBuiltIn(new ThreadCreateProcedure());
library.addBuiltIn(new ThreadInterruptProcedure());
library.addBuiltIn(new ThreadListProcedure());
+ library.addBuiltIn(new TypeListProcedure());
library.addBuiltIn(new UserChangeProcedure());
library.addBuiltIn(new UserCheckAccessProcedure());
library.addBuiltIn(new UserListProcedure());
@@ -0,0 +1,122 @@
+/*
+ * RapidContext <http://www.rapidcontext.com/>
+ * Copyright (c) 2007-2012 Per Cederberg. All rights reserved.
+ *
+ * This program is free software: you can redistribute it and/or
+ * modify it under the terms of the BSD license.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * See the RapidContext LICENSE.txt file for more details.
+ */
+
+package org.rapidcontext.app.proc;
+
+import org.rapidcontext.core.data.Array;
+import org.rapidcontext.core.proc.Bindings;
+import org.rapidcontext.core.proc.CallContext;
+import org.rapidcontext.core.proc.Procedure;
+import org.rapidcontext.core.proc.ProcedureException;
+import org.rapidcontext.core.security.Restricted;
+import org.rapidcontext.core.storage.Path;
+import org.rapidcontext.core.type.Type;
+
+/**
+ * The built-in type list procedure.
+ *
+ * @author Per Cederberg
+ * @version 1.0
+ */
+public class TypeListProcedure implements Procedure, Restricted {
+
+ /**
+ * The app object storage path.
+ */
+ public static final Path PATH_APP = new Path("/type/");
+
+ /**
+ * The procedure name constant.
+ */
+ public static final String NAME = "System.Type.List";
+
+ /**
+ * The default bindings.
+ */
+ private Bindings defaults = new Bindings();
+
+ /**
+ * Creates a new app list procedure.
+ */
+ public TypeListProcedure() {
+ this.defaults.seal();
+ }
+
+ /**
+ * Checks if the currently authenticated user has access to this
+ * object.
+ *
+ * @return true if the current user has access, or
+ * false otherwise
+ */
+ public boolean hasAccess() {
+ return true;
+ }
+
+ /**
+ * Returns the procedure name.
+ *
+ * @return the procedure name
+ */
+ public String getName() {
+ return NAME;
+ }
+
+ /**
+ * Returns the procedure description.
+ *
+ * @return the procedure description
+ */
+ public String getDescription() {
+ return "List all registered storage data types.";
+ }
+
+ /**
+ * Returns the bindings for this procedure. If this procedure
+ * requires any special data, adapter connection or input
+ * argument binding, those bindings should be set (but possibly
+ * to null or blank values).
+ *
+ * @return the bindings for this procedure
+ */
+ public Bindings getBindings() {
+ return defaults;
+ }
+
+ /**
+ * Executes a call of this procedure in the specified context
+ * and with the specified call bindings. The semantics of what
+ * the procedure actually does, is up to each implementation.
+ * Note that the call bindings are normally inherited from the
+ * procedure bindings with arguments bound to their call values.
+ *
+ * @param cx the procedure call context
+ * @param bindings the call bindings to use
+ *
+ * @return the result of the call, or
+ * null if the call produced no result
+ *
+ * @throws ProcedureException if the call execution caused an
+ * error
+ */
+ public Object call(CallContext cx, Bindings bindings)
+ throws ProcedureException {
+ Type[] types = Type.findAll(cx.getStorage());
+ Array res = new Array(types.length);
+ for (int i = 0; i < types.length; i++) {
+ res.add(types[i].serialize().copy());
+ }
+ return res;
+ }
+}
@@ -1,6 +1,6 @@
/*
* RapidContext <http://www.rapidcontext.com/>
- * Copyright (c) 2007-2010 Per Cederberg. All rights reserved.
+ * Copyright (c) 2007-2012 Per Cederberg. All rights reserved.
*
* This program is free software: you can redistribute it and/or
* modify it under the terms of the BSD license.
@@ -15,6 +15,7 @@
package org.rapidcontext.core.type;
import java.lang.reflect.Constructor;
+import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -77,12 +78,29 @@
* null if not found
*/
public static Type find(Storage storage, String id) {
- Object obj = storage.load(PATH.descendant(new Path(id)));
-
+ Object obj = storage.load(PATH.descendant(new Path(id)));
return (obj instanceof Type) ? (Type) obj : null;
}
/**
+ * Searches for all types in the storage.
+ *
+ * @param storage the storage to search in
+ *
+ * @return an array of all types found
+ */
+ public static Type[] findAll(Storage storage) {
+ Object[] objs = storage.loadAll(PATH);
+ ArrayList list = new ArrayList(objs.length);
+ for (int i = 0; i < objs.length; i++) {
+ if (objs[i] instanceof Type) {
+ list.add(objs[i]);
+ }
+ }
+ return (Type[]) list.toArray(new Type[list.size()]);
+ }
+
+ /**
* Returns a constructor for creating a Java object instance. If
* no object type or initializer was found, or if an error
* occurred, null is returned. This method will lookup the

0 comments on commit 2722d31

Please sign in to comment.