Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    Best Practice: Modifying DataSources via addDynamicDSGenerator

    Hi Isomorphic,

    I need to dynamically modify a DataSource depending on some configuration stored in the database (using 5.1d).
    I managed to call my DynamicDSGenerator with the information I found in the QSG and the forums. The only thing the QSG left unclear IMHO is what you wrote in this post and perhaps the information on ServerInit (my web.xml only had one way of loading Init, perhaps because I started from a 4.0/4.1 builtInDS).

    On modifying the DataSource itself, this is my java code:
    Code:
    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    
    import org.w3c.dom.Document;
    
    import com.isomorphic.datasource.DSRequest;
    import com.isomorphic.datasource.DataSource;
    import com.isomorphic.datasource.DynamicDSGenerator;
    
    public class RegisterDS extends HttpServlet {
        private static final long serialVersionUID = 7849924835392267735L;
    
        @Override
        public void init() throws ServletException {
            System.out.println("Called RegisterDS");
            DynamicDSGenerator myDynamicDSGenerator = new DynamicDSGenerator() {
                @Override
                public DataSource getDataSource(String id, DSRequest dsRequest) {
                    DataSource ds = null;
                    try {
                        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
                        ServletContext servletContext = getServletContext();
                        String strAbsolutePath = servletContext.getRealPath("ds");
    
                        Document doc = builder.parse(strAbsolutePath + "/V_LEADUPLOAD.ds.xml");
                        doc.getDocumentElement().setAttribute("foobar", "foobar");
                        ds = DataSource.fromXML(doc);
    
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                    return ds;
                }
            };
            DataSource.addDynamicDSGenerator(myDynamicDSGenerator, "V_LEADUPLOAD");
        }
    }
    First of all, this works, which is great and an impressive feature (I can see the "foobar" in the DSLoader response). I have two questions though:
    1. Is this the correct way to get the path to the .ds.xml file I use as template?
    2. Is the configuration always xml-modification and DataSource.fromXML(doc) in the end? Or is there some other java-only way (á la "myField = new DSField(); myField.set...(); myDS.setFields(myField); return myDS;")
    Thank you & Best regards
    Blama

    #2
    1. If you choose to use templates, you can put them anywhere you want and provide the path anyway you'd like. However, if the templates are never intended to be used unmodified, it's probably best not to put them with normal .ds.xml files, but instead in another directory.

    2. You always need to produce an XML definition. You can produce that XML definition any way you want; starting from a template and modifying it is not a requirement of the system. For example, you might have no XML on disk, and be loading the definitions from a database.

    Comment


      #3
      Hi Isomorphic,

      thanks for the fast answer.

      Originally posted by Isomorphic View Post
      2. You always need to produce an XML definition. You can produce that XML definition any way you want; starting from a template and modifying it is not a requirement of the system. For example, you might have no XML on disk, and be loading the definitions from a database.
      OK, but with the information from the DB I'll always modify an XML document (then in memory only) and finally convert this this to a DS via DataSource.fromXML(doc)? Or is there an other way to build the DataSource object as well?

      Best regards
      Blama

      Comment


        #4
        Again, the API just requires an XML document as output. There is no assumption that there is a template on disk or anywhere else. The entire DataSource definition could be in a database, with no on-the-fly modication by code in the registered DynamicDSGenerator - just retrieval. Or the entire definition could be programmatically generated without the use of a template. The API just requires that you produce an XML doc, and that's it.

        Comment

        Working...
        X