Skip to content

Commit

Permalink
Make jar creation work
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
Jesús Gómez committed Jul 25, 2021
1 parent 6c45ead commit 740654f
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 17 deletions.
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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

Expand Down
10 changes: 3 additions & 7 deletions deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 25 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,31 @@
<artifactId>clojure</artifactId>
<version>1.10.3</version>
</dependency>
<dependency>
<groupId>org.apache.mina</groupId>
<artifactId>mina-core</artifactId>
<version>2.0.21</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.31</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.31</version>
</dependency>
<dependency>
<groupId>org.apache.ftpserver</groupId>
<artifactId>ftplet-api</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.ftpserver</groupId>
<artifactId>ftpserver-core</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
Expand All @@ -39,10 +64,6 @@
<id>clojars</id>
<url>https://repo.clojars.org/</url>
</repository>
<repository>
<id>sonatype</id>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
</repository>
</repositories>
<distributionManagement>
<repository>
Expand Down
20 changes: 16 additions & 4 deletions src/jgomo3/ftp_hog.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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.)
Expand Down Expand Up @@ -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)))

0 comments on commit 740654f

Please sign in to comment.