Current state of Rocker. rocker
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 2 Aug 2012 15:13:12 +0000 (17:13 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 2 Aug 2012 15:13:12 +0000 (17:13 +0200)
src/main/java/net/pterodactylus/demoscenemusic/rocker/ArtistRocker.java [new file with mode: 0644]
src/main/java/net/pterodactylus/utils/rocker/Parameter.java [new file with mode: 0644]
src/main/java/net/pterodactylus/utils/rocker/RockerServlet.java [new file with mode: 0644]
src/main/java/net/pterodactylus/utils/rocker/Rocklet.java [new file with mode: 0644]

diff --git a/src/main/java/net/pterodactylus/demoscenemusic/rocker/ArtistRocker.java b/src/main/java/net/pterodactylus/demoscenemusic/rocker/ArtistRocker.java
new file mode 100644 (file)
index 0000000..739bc0f
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ * DemosceneMusic - ArtistRocker.java - Copyright © 2012 David Roden
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * 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
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.demoscenemusic.rocker;
+
+import net.pterodactylus.util.web.Method;
+import net.pterodactylus.utils.rocker.Parameter;
+import net.pterodactylus.utils.rocker.Rocklet;
+
+/**
+ * TODO
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class ArtistRocker {
+
+       @Rocklet(path = "addArtist", methods = Method.POST)
+       public void addArtist(@Parameter("name") String name) {
+
+       }
+
+       @Rocklet(path = "editArtist", methods = Method.POST)
+       public void editArtist(@Parameter("artist") String id, @Parameter("name") String name) {
+
+       }
+
+}
diff --git a/src/main/java/net/pterodactylus/utils/rocker/Parameter.java b/src/main/java/net/pterodactylus/utils/rocker/Parameter.java
new file mode 100644 (file)
index 0000000..0682fc7
--- /dev/null
@@ -0,0 +1,36 @@
+/*
+ * DemosceneMusic - Parameter.java - Copyright © 2012 David Roden
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * 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
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.utils.rocker;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * TODO
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.PARAMETER)
+public @interface Parameter {
+
+       String value();
+
+}
diff --git a/src/main/java/net/pterodactylus/utils/rocker/RockerServlet.java b/src/main/java/net/pterodactylus/utils/rocker/RockerServlet.java
new file mode 100644 (file)
index 0000000..601c3bf
--- /dev/null
@@ -0,0 +1,131 @@
+/*
+ * DemosceneMusic - RockerServlet.java - Copyright © 2012 David Roden
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * 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
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.utils.rocker;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import net.pterodactylus.util.collection.IterableWrapper;
+import net.pterodactylus.util.collection.IteratorWrapper;
+import net.pterodactylus.util.collection.filter.Filter;
+import net.pterodactylus.util.web.Method;
+
+/**
+ * TODO
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class RockerServlet extends HttpServlet {
+
+       private final Map<String, Map<Method, java.lang.reflect.Method>> rocklets = new HashMap<String, Map<Method, java.lang.reflect.Method>>();
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       @SuppressWarnings("synthetic-access")
+       public void init() throws ServletException {
+               for (String name : IterableWrapper.wrap(new InitParameters()).filter(new Filter<String>() {
+
+                       @Override
+                       public boolean filterObject(String object) {
+                               return object.startsWith("rocklet.");
+                       }
+
+               })) {
+                       String rockletClassName = getInitParameter(name);
+                       try {
+                               Class<?> rockletClass = Class.forName(rockletClassName);
+                               for (java.lang.reflect.Method rockletMethod : rockletClass.getMethods()) {
+                                       Rocklet rockletAnnotation = rockletMethod.getAnnotation(Rocklet.class);
+                                       String path = rockletAnnotation.path();
+                                       if (!rocklets.containsKey(path)) {
+                                               rocklets.put(path, new HashMap<Method, java.lang.reflect.Method>());
+                                       }
+                                       for (Method method : rockletAnnotation.methods()) {
+                                               if (rocklets.get(path).containsKey(method)) {
+                                                       throw new RuntimeException("Duplicate method " + method + " for " + path + "!");
+                                               }
+                                               rocklets.get(path).put(method, rockletMethod);
+                                       }
+                               }
+                       } catch (ClassNotFoundException cnfe1) {
+                       }
+               }
+       }
+
+       //
+       // HTTPSERVLET METHODS
+       //
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       protected void service(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
+               httpServletRequest.setCharacterEncoding("UTF-8");
+               super.service(httpServletRequest, httpServletResponse);
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       protected void doPost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
+       }
+
+       //
+       // PRIVATE METHODS
+       //
+
+       private String getPath(HttpServletRequest httpServletRequest) {
+               String path = httpServletRequest.getPathInfo();
+               if (path.startsWith("/")) {
+                       return path.substring(1);
+               }
+               return path;
+       }
+
+       private class InitParameters implements Iterable<String> {
+
+               /**
+                * {@inheritDoc}
+                */
+               @Override
+               @SuppressWarnings("unchecked")
+               public Iterator<String> iterator() {
+                       return IteratorWrapper.wrap(getInitParameterNames());
+               }
+
+       }
+
+}
diff --git a/src/main/java/net/pterodactylus/utils/rocker/Rocklet.java b/src/main/java/net/pterodactylus/utils/rocker/Rocklet.java
new file mode 100644 (file)
index 0000000..b17f815
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+ * DemosceneMusic - Rocklet.java - Copyright © 2012 David Roden
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * 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
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.utils.rocker;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+import net.pterodactylus.util.web.Method;
+
+/**
+ * TODO
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.METHOD)
+public @interface Rocklet {
+
+       String path();
+
+       Method[] methods();
+
+}