OpenCms Creating resource depth analysis

Source: Internet
Author: User

Introduced:

In OpenCms, some resources are just created, some of the resources are created and published, and what do they do when they are created, and how they are stored, which is the topic to be addressed in this article.


Analysis:

As an example of the simplest text file, when we create a text file with a wizard in a directory (for example,/charlesstudy), the following:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/54/2A/wKioL1R66MXDg1S8AAC9XpznmZI302.jpg "title=" 23.png "alt=" Wkiol1r66mxdg1s8aac9xpznmzi302.jpg "/>

It sends a request for POST http://localhost:8080/opencms/opencms/system/workplace/commons/newresource.jsp


The server-side response entry is the case Action_submitform branch within the Cmsnewresource Actiondialog () method:

Public void actiondialog ()  throwsjspexception, servletexception, ioexception {          super.actiondialog ();          switch  (Getaction ())  {             caseACTION_SUBMITFORM:                 actioncreateresource ();                 if  (isresourcecreated ())  {                     actioneditproperties ();// redirects only if the edit propertiesoption was checked                 }                 break, ..... 


In the Actioncreateresource () method:

Publicvoidactioncreateresource ()  throws JspException {          try {            //  calculate the new resource title property value             string title = computenewtitleproperty ();              // create the full  resource name            string  Fullresourcename =computefullresourcename ();              // create the Title and Navigation properties if  configured            i_cmsresourcetype  Restype = opencms.getresourcemanAger (). getResourceType (Getparamnewresourcetype ());             list<cmsproperty> properties = createresourceproperties (FullResourceName,  restype.gettypename (),  title);              // create the resource                         getcms (). CreateResource ( Fullresourcename, restype.gettypeid (),  null, properties);             setparamresource (Fullresourcename);              setresourcecreated (True);         } catch  (throwable e)  {              // error creating file, show error dialog             includeerrorpage (this, e);         }

As we can see, on the macro, it first gets the resource title (title), the full path of the resource (fullresourcename), the resource type (plain, its type=1), and the additional properties of the configuration (properties), as follows:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/54/2B/wKioL1R66mOiWGc9AAG9dpkJY5c702.jpg "title=" 31.png "alt=" Wkiol1r66moiwgc9aag9dpkjy5c702.jpg "/>

Then call Getcms (). Createresource (Fullresourcename,restype.gettypeid (), null,properties) to do the creation work and it will delegate A_ The Createresource () of the Cmsresourcetype class is used to create the resource, in which it then delegates the Cmssecuritymanager Createresource () method to make the existence checksum of the resource to be created. The Cmsdrivermanager Createresource () method will eventually be called to do the actual resource creation work, as follows:

Public cmsresource createresource (        CmsDbContext  dbc,        string resourcename,         inttype,        byte[] content,         list<cmsproperty> properties)  throws CmsException, Cmsillegalargumentexception {         string targetname  = resourcename;         if  (content ==  NULL) {            // name based  resource creation must have a content             content = newbyte[0];        }         intsize;         if  ( Cmsfolder.isfoldertype (type))  {            //  must cut of trailing  '/'  for folder creation             if  (Cmsresource.isfolder (targetName))  {                 targetName =  Targetname.substring (0, targetname.length ()  - 1);             }            size  = -1;        } else {             size = content.length;         }         // create a new resource         cmsresource newresource = new cmsresource (Cmsuuid.getnulluuid (), //  uuids will be  "corrected"  later             cmsuuid.getnulluuid (),             targetName,            type,             cmsfolder.isfoldertype (type),             0,             dbc.currentproject (). Getuuid (),             cmsresource.state_new,            0,      &nBsp;      dbc.currentuser (). GetId (),             0,             Dbc.currentuser (). GetId (),             cmsresource.date_released_default,             Cmsresource.date_expired_default,            1,             size,             0, // version number does not matter  since it will be computed later             0); // content time will be corrected later         &nBsp;return createresource (dbc, targetname, newresource, content, properties,  false);    }

This method is divided into 2 parts, one is the prototype phase (the part labeled//create a new resource in the code), and the second is the actual creation phase (which calls the overloaded Createresource () method to create the real resource and write the database), which we analyze in detail.


Discussion point 1: The prototype phase of resource creation:

It will simply fill in the general information that is brought in from Actioncreateresource, such as Project Uuid,type,path, and then all other information is empty, and this step creates the resource we call the resource prototype, which creates a "live" In the computer's memory, it does not persist, and its information is as follows:

[Org.opencms.file.CmsResource, Path:/sites/default/charles_study/text-text-file-5,structure ID 00000000-0000-0000-0000-000000000000, Resource id:00000000-0000-0000-0000-000000000000, type id:1, Folder:false,  flags:0,project:ae97ff1d-7824-11e4-8c0e-28e347ffa92b, state:2, date Created:thu Jan01 08:00:00 CST 1970, User created: C300ba5c-01e8-3727-b305-5dcc9ccae1ee, Datelastmodified:thu Jan 08:00:00 CST 1970, User lastmodified: C300ba5c-01e8-3727-b305-5dcc9ccae1ee, date Released:thu Jan 08:00:00 CST1970, date Expired:sun 15:12:55 CST 29 2278994, date Content:thu Jan 0108:00:00 CST 1970, size:0, sibling count:1, version:0]


Discussion point 2: Actual resource creation phase:

This is a lot of things, because of the length of the relationship, I do not post all the code, from the macro it generally does the following things:

A. Get the parent directory (ParentFolder) to create the resource:

[org.opencms.file.cmsfolder, path:/sites/default/charles_study/,  structure id 4bf8b750-785d-11e4-8289-28e347ffa92b,resource id:  4bf8b751-785d-11e4-8289-28e347ffa92b, type id: 0, folder: true,flags: 0,  Project: ae97ff1d-7824-11e4-8c0e-28e347ffa92b, state: 2, datecreated: sun nov  30 14:51:23 CST 2014, user created:c300ba5c-01e8-3727-b305-5dcc9ccae1ee,  Date lastmodified: sun nov 30 14:51:35cst 2014, user lastmodified:  c300ba5c-01e8-3727-b305-5dcc9ccae1ee, datereleased: Thu Jan 01 08:00:00  Cst 1970, date expired: sun aug 17 15:12:55 cst292278994, date  content: thu jan 01 07:59:59 cst 1970, size: -1, sibling &NBSP;COUNT:1,&NBSP;VERSION:&NBSP;1] 


B. Determine the lock of the parent directory. because the parent directory is locked by another user, we cannot create resources under this directory. Conversely, if the parent directory is not locked , or if the owner of the lock is the current user, then the resource can be created under it.


C. Determine if the name of the resource uses the forbidden character.


D. Build a new resource object in memory. with Structureid,resourceid and other information, the new objects are now built to be much richer than the prototype stage:

[Org.opencms.file.CmsResource, PATH:/SITES/DEFAULT/CHARLES_STUDY/TEXT-TEXT-FILE-5, structure id5b97ca24-786d-11e4-8289-28e347ffa92b, Resource id:5d577b85-786d-11e4-8289-28e347ffa92b, type id:1, Folder:false,  flags:0,project:ae97ff1d-7824-11e4-8c0e-28e347ffa92b, state:2, date Created:thu Jan01 08:00:00 CST 1970, User created: C300ba5c-01e8-3727-b305-5dcc9ccae1ee, Datelastmodified:thu Jan 08:00:00 CST 1970, User lastmodified: C300ba5c-01e8-3727-b305-5dcc9ccae1ee, date Released:thu Jan 08:00:00 CST1970, date Expired:sun 15:12:55 CST 29 2278994, date Content:thu Jan 0108:00:00 CST 1970, size:0, sibling count:1, version:0]


E. Write the resource object to the database. the code written is located in Cmsvfsdriver's Createresource () method, which, on the macro level, does a few more steps:

    1. number path checking at the library level

    2. Set the status and modification date of a resource

    3. update the In-memory resource object to the Cms_offline_structure table. The SQL statement that it executes is:

INSERT into Cms_offline_structure (structure_id,resource_id,resource_path,structure_state,date_released,date_ expired,parent_id,structure_version) VALUES (' 273e3066-786e-11e4-8289-28e347ffa92b ', ' 273e3067-786e-11e4-8289-28e347ffa92b ', '/sites/default/charles_study/test-text-file-5 ', 2,0,9223372036854775807, ' 4bf8b750-785d-11e4-8289-28e347ffa92b ', 0)

We looked at the database to see what we did today, all of the resources that were not released:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/54/2C/wKiom1R67n7QCLnJAAMDNwGgsa4687.jpg "title=" 31.png "alt=" Wkiom1r67n7qclnjaamdnwggsa4687.jpg "/>

They and we see in the CMS workplace is exactly the same:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/54/2C/wKiom1R67rDjT76LAAIQzgt1sRs679.jpg "title=" 32.png "alt=" Wkiom1r67rdjt76laaiqzgt1srs679.jpg "/>

4. Create the file contents. It is implemented through the Createcontent () method of the Cmsvfsdriver (), and is read directly to the file contents <2kb, and the contents of the >2kb file are read by opening an input stream, and the content of the file is then written to Cms_ In the Offline_contents table, the SQL statement that it executes is:

INSERT into Cms_offline_contents (resource_id,file_content) VALUES (' 273e3067-786e-11e4-8289-28e347ffa92b ', X ')

The files that are written are stored in blob form:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/54/2C/wKiom1R678fxPe5uAAFMFdwWX7Q548.jpg "title=" 33.png "alt=" Wkiom1r678fxpe5uaafmfdwwx7q548.jpg "/>

5. Fix all relationships that reference the resource. it is implemented by calling the Repairbrokenrelations () method, which updates the Cms_offline_resource_relations table. The SQL statement that it executes is:

UPDATE Cms_offline_resource_relations left joincms_offline_structure on cms_offline_resource_relations. relation_target_id =cms_offline_structure. structure_id SET relation_target_id = ' 273e3066-786e-11e4-8289-28e347ffa92b ' wherecms_offline_resource_relations. Relation_target_path = '/sites/default/charles_study/test-text-file-5 ' andcms_offline_structure. STRUCTURE_ID is NULL


F. Lock the new resource you just created, lock type is exclusive lock (cmslocktype.exclusive)


Summarize:

From the above process can be seen, there are the following important conclusions:

    1. Resource resource is created in two stages, in which the resource objects created in the prototype phase exist only in memory, and the resource objects created during the actual creation phase are both present in memory and persisted into the database.

    2. Aside from some not too critical links, such as resource permissions check, resource uniqueness check, lock check, the creation phase will mainly update Cms_offline_structure,cms_offline_contents,cms_offline_resource_ Relations three tables, the effect of these three tables is to store the structure of the unpublished resources, the actual content, the reference relationship.

This article is from the "cohesion of parallel Lines" blog, please be sure to keep this source http://supercharles888.blog.51cto.com/609344/1584798

OpenCms Creating resource depth analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.