Java webapp customization strategies

Part of my webapp includes uploading image files. On the production server, files must be written to / somepath _on_production_server / images. For local development, I want to write files to / some _different_path / images.

What is the best way to deal with these configuration differences?

One of the important requirements is the following: I do not want to communicate with the production server at all, I just want to be able to deploy the military file and make it work. Therefore, I do not want to use any technique that will require me to interact with environment variables / classpath / etc. on a production machine. I am fine with the setup on my local machine.

I imagine two possible general approaches:

  • loading a special configuration file "dev" at runtime if certain conditions are met (environment variable / classpath / etc)
  • switch toggle during the build process (maybe maven profiles?)
+3
source share
3 answers

Simple things, such as String, can be declared as environment entries in web.xmland received through JNDI. The following is an example with env-entrythe name "imagePath".

<env-entry> 
    <env-entry-name>imagePath</env-entry-name> 
    <env-entry-value>/somepath_on_production_server/images</env-entry-value> 
    <env-entry-type>java.lang.String</env-entry-type> 
</env-entry>

To access the properties of your Java code, do a JNDI search:

// Get a handle to the JNDI environment naming context
Context env = (Context)new InitialContext().lookup("java:comp/env");

// Get a single value
String imagePath = (String)env.lookup("imagePath");

This is usually done in the old style ServiceLocator, where you must cache the value for this key.

- .


maven ( web.xml).

+2

WAR, , , . JNDI.

String uploadLocation = System.getProperty("upload.location", "c:/dev");

()

+1

web.xml

InputStream ldapConfig = getClass().getResourceAsStream(
          "/ldap-jndi.properties");
      Properties env = new Properties();
      try {
        env.load(ldapConfig);
      } finally {
        if (ldapConfig != null) {
          ldapConfig.close();
        }
      }
+1

Source: https://habr.com/ru/post/1759225/


All Articles