From 740654fdfef432d5d8f5c52da6ae5a46512ab665 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20G=C3=B3mez?= Date: Sun, 25 Jul 2021 14:59:14 -0230 Subject: [PATCH] Make jar creation work Creating the jar was not possible because the code was trying to read a resource and use it as a File object for the PropertiesUserManagerFactory setFile method (Apache ftpserver). ftpserver already managed the case when it had to read the resource from a jar file, so I shouldn't have to create the File from the resource myself, but just create an abstract File object with the relative path of the resource. That was enough for ftpserver to find the resource in the Jar file. Check the loadFromFile method of PropertiesUserManager class of Apache ftpserver to see how they do it (using getResourceAsStream). --- README.md | 18 ++++++++++++++++-- deps.edn | 10 +++------- pom.xml | 29 +++++++++++++++++++++++++---- src/jgomo3/ftp_hog.clj | 20 ++++++++++++++++---- 4 files changed, 60 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 23caba6..1ed36b9 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ but super simple. Let's see. Run the project directly, via `:exec-fn`: - $ clojure -X:run-x + $ clojure -X:run [main] INFO org.apache.ftpserver.impl.DefaultFtpServer - FTP server started Then, from an FTP client you can connect to the server on port 2221: @@ -48,7 +48,21 @@ Then, from an FTP client you can connect to the server on port 2221: You can specify the port with the `:port` option: - $ clojure -X:run-x :port 2222 + $ clojure -X:run :port 2222 + +You can also build the `.jar` and then use plain Java to run it. + +To create the file `ftp-hog.jar` run: + + $ clojure -X:uberjar + +Then, run it with: + + $ java -jar ftp-hog.jar + +Or passing the port as first argument (Notice you don't have to say `:port`) + + $ java -jar frp-hog.jar 2222 ## License diff --git a/deps.edn b/deps.edn index 51cf693..2fc3584 100644 --- a/deps.edn +++ b/deps.edn @@ -6,13 +6,9 @@ org.apache.ftpserver/ftplet-api {:mvn/version "1.1.1"} org.apache.ftpserver/ftpserver-core {:mvn/version "1.1.1"}} :aliases - {:run-m {:main-opts ["-m" "jgomo3.ftp-hog"]} - :run-x {:ns-default jgomo3.ftp-hog - :exec-fn run - :exec-args {:port 2221 - :path "/tmp" - :user "anonymous" - :password ""}} + {:run {:ns-default jgomo3.ftp-hog + :exec-fn run + :exec-args {}} :test {:extra-paths ["test"] :extra-deps {org.clojure/test.check {:mvn/version "1.1.0"} io.github.cognitect-labs/test-runner diff --git a/pom.xml b/pom.xml index e2e53fe..f177399 100644 --- a/pom.xml +++ b/pom.xml @@ -30,6 +30,31 @@ clojure 1.10.3 + + org.apache.mina + mina-core + 2.0.21 + + + org.slf4j + slf4j-api + 1.7.31 + + + org.slf4j + slf4j-simple + 1.7.31 + + + org.apache.ftpserver + ftplet-api + 1.1.1 + + + org.apache.ftpserver + ftpserver-core + 1.1.1 + src @@ -39,10 +64,6 @@ clojars https://repo.clojars.org/ - - sonatype - https://oss.sonatype.org/content/repositories/snapshots/ - diff --git a/src/jgomo3/ftp_hog.clj b/src/jgomo3/ftp_hog.clj index 3f05292..579ecc4 100644 --- a/src/jgomo3/ftp_hog.clj +++ b/src/jgomo3/ftp_hog.clj @@ -3,7 +3,8 @@ [clojure.string :as str]) (:import [org.apache.ftpserver FtpServer FtpServerFactory] [org.apache.ftpserver.listener ListenerFactory] - [org.apache.ftpserver.usermanager PropertiesUserManagerFactory])) + [org.apache.ftpserver.usermanager PropertiesUserManagerFactory]) + (:gen-class)) (defn create-server [listener user-manager] (-> (doto (FtpServerFactory.) @@ -35,13 +36,24 @@ msj (str/join "\n" msj-lines)] (println msj))) -(defn run [{:keys [port path user password] :as opts}] +(defn run [{:keys [port path user password] :or {port 2221 path "/tmp" user "anonymous" password ""}}] (do - (welcome opts) + (welcome {:port port :path path :user user :password password}) (let [user-manager (-> "conf/users.properties" - io/resource io/file create-user-manager) listener (create-listener port) server (create-server listener user-manager)] (.start server)))) + +(defn- assoc-if-some + ([m k val] (assoc-if-some m k val identity)) + ([m k val tx] + (cond-> m + (some? val) (assoc k (tx val))))) + +(defn -main [& args] + (let [port (first args) + opts (assoc-if-some {} :port port (fn [val] (Integer/parseInt val)))] + ;; opts #_ + (run opts)))