DBA Data[Home] [Help]

PACKAGE BODY: APPS.FND_GSM_UTIL

Source


1 package body fnd_gsm_util as
2 /* $Header: AFCPGUTB.pls 120.11 2011/08/10 19:58:50 ckclark ship $ */
3 
4 
5   ctx_file_clob clob := null;
6   contextfile_handle varchar2(128);
7 
8   --
9   -- GENERIC_ERROR (Internal)
10   --
11   -- Set error message and raise exception for unexpected sql errors.
12   --
13   procedure GENERIC_ERROR(routine in varchar2,
14                           errcode in number,
15                           errmsg in varchar2) is
16     begin
17       fnd_message.set_name('FND', 'SQL_PLSQL_ERROR');
18       fnd_message.set_token('ROUTINE', routine);
19       fnd_message.set_token('ERRNO', errcode);
20       fnd_message.set_token('REASON', errmsg);
21     end;
22 
23   /* returns TRUE if FND_DATABASES table exists else FALSE */
24   function db_model_exists return boolean is
25     TableNotFound EXCEPTION;
26     PRAGMA EXCEPTION_INIT(TableNotFound, -942);
27     cnt number;
28   begin
29     execute immediate 'select count(*) from fnd_databases' into cnt;
30 
31     return TRUE;
32 
33    exception
34       when TableNotFound then
35          return FALSE;
36 
37   end;
38 
39 
40   --
41   -- procedure
42   --   Append_ctx_fragment
43   --
44   -- Purpose
45   --   Used to upload a context file into a clob for parsing.
46   --   A temporary clob is created on the first call.  This clob
47   --   will be freed when the upload_context_file procedure is called.
48   --
49   -- In Arguments:
50   --   buffer - Context file, or fragment of a context file.
51   --
52   -- Out Arguments:
53   --   retcode - 0 on success, >0 on error.
54   --   message - Error message, up to 2000 bytes.
55   --
56   procedure append_ctx_fragment(buffer  in  varchar2,
57                                 retcode out nocopy number,
58                                 message out nocopy varchar2) is
59     buffer_length number;
60   begin
61 
62     -- Create the temp clob if necessary
63     if ctx_file_clob is null then
64       dbms_lob.createtemporary(ctx_file_clob, true, dbms_lob.session);
65     end if;
66 
67     -- Append the buffer to the clob
68 if buffer IS NULL then
69    retcode := 0;
70    return;
71 else
72     buffer_length := length(buffer);
73     if (buffer_length > 0) then
74     	dbms_lob.writeappend (ctx_file_clob, buffer_length, buffer);
75     	retcode := 0;
76         return;
77     else
78 	retcode := 0;
79   	return;
80     end if;
81 
82 end if;
83 
84   exception
85     when others then
86       fnd_message.set_name('FND', 'SQL_PLSQL_ERROR');
87       fnd_message.set_token('ROUTINE', 'FND_GSM_UTIL.APPEND_CTX_FRAGMENT');
88       fnd_message.set_token('ERRNO', SQLCODE);
89       fnd_message.set_token('REASON', SQLERRM);
90       message :=  fnd_message.get;
91       retcode := 1;
92 
93   end append_ctx_fragment;
94 
95 
96 
97   --
98   -- Procedure
99   --   upload_context_file
100   --
101   -- Purpose
102   --   Parse the context file stored in the temporary clob and create
103   --   the appropriate service instance definitions for GSM.  The clob is
104   --   created by the append_ctx_fragment
105   --
106   -- In Arguments:
107   --   filepath - Full path to the context file.  Used for bookkeeping.
108   --   context_type - 'APPS' Application middle tier,
109   --                  'DATABASE' Database context
110   --   file_type  - 'CONTEXT' - Instantiated Context file
111   --                'TEMPLATE' - Template file for Context file.
112   --
113   -- Out Arguments:
114   --   retcode - 0 on success, >0 on error.
115   --   message - Error message, up to 2000 bytes.
116   --
117   procedure upload_context_file(filepath in varchar2,
118                                 retcode out nocopy number,
119                                 message out nocopy varchar2,
120 				context_type in varchar2 default 'APPS',
121 				file_type in varchar2 default 'CONTEXT') is
122   primary_node  varchar2(30);
123   platform_name varchar2(30);
124   domain_name   varchar2(64);
125   tmp_node_name varchar2(100);
126   reports_port  varchar2(20);
127   forms_port    varchar2(20);
128   methost_node  varchar2(30);
129   met_data_port varchar2(20);
130   met_req_port  varchar2(20);
131   web_port      varchar2(20);
132   web_pls_port  varchar2(20);
133   apache_top    varchar2(512);
134   web_pid_file  varchar2(510);
135   web_pls_pid_file  varchar2(510);
136   mod_pls_exists boolean;
137   tech_stack    varchar2(128);
138   logs_dir      varchar2(510);
139 
140   message_name varchar2(30);
141   service_name varchar2(255);
142   short_name   varchar2(30);
143   ctrl_script  varchar2(512);
144   port         varchar2(20);
145   status       varchar2(30);
146   service_handle varchar2(8);
147   parameters     varchar(2000);
148   process_name   varchar2(512);
149   process_log    varchar2(512);
150   reports_short_name varchar2(128);
151 
152   queue_id          number;
153   queue_appl_id     number;
154   fcq_enabled_flag  varchar2(1);
155 
156   my_parser    xmlParser.parser;
157   my_doc       xmlDOM.DOMDocument;
158   my_nodelist  xmlDOM.DOMNodeList;
159   my_nodelist2 xmlDOM.DOMNodeList;
160   my_contextlist xmlDOM.DOMNodeList;
161   my_node      xmlDOM.DOMNode;
162   my_node2     xmlDOM.DOMNode;
163   my_context   xmlDOM.DOMNode;
164   my_nodemap   xmlDOM.DOMNamedNodeMap;
165   my_attr      xmlDOM.DOMAttr;
166   my_element   xmlDOM.DOMElement;
167   server_type  varchar2(128);
168   oh_type      varchar2(128);
169   i            number;
170   listlength   number;
171   file_found   boolean := FALSE;
172   my_cdata     xmlDOM.DOMCharacterData;
173 
174   sr_num      varchar2(128);
175   f_sr_num    number;
176   db_sr_num   number;
177   file_ver     varchar2(128);
178   db_ver_str   varchar2(30);
179   db_ver       number;
180   f_ver        number;
181   context_name varchar2(512);
182   element_name varchar2(40);
183   missing_elem      exception;
184   exceeded_length   exception;
185   null_context_name exception;
186   metadata_file     exception;
187   filesys_low_ver   exception;
188   packagenotexists  exception;
189   PRAGMA EXCEPTION_INIT(packagenotexists, -06550);
190   sql_str      varchar2(1000);
191   db_name      varchar2(8);
192   db_domain    varchar2(255);
193   db_host      varchar2(255);
194   db_port      number;
195   db_sid       varchar2(255);
196   ret_val      number;
197   inst_name    varchar2(16);
198   inst_num     number;
199   c_null       varchar2(1);
200   n_null       number;
201   v_result     integer;
202   comp         number;
203 
204   begin
205 
206 
207     if ctx_file_clob is null then
208       retcode := 1;
209       message := 'Developer error: Must call append_ctx_fragment first.';
210       return;
211     end if;
212 
213     /* Parse Context File */
214     my_parser := xmlparser.newParser;
215     xmlparser.parseClob(my_parser, ctx_file_clob);
216 --    dbms_lob.freetemporary(ctx_file_clob);
217 --    ctx_file_clob := null;
218     my_doc := xmlparser.getdocument(my_parser);
219 
220 
221     /* TO DO:  Below we search for each element top down.   The
222      * performance of this procedure can be increased if we use
223      * our knowledge of the file to avoid restarting at the top
224      * for each element.
225      * I don't know how significant of an improvement it will be.
226      * Since this program will run very infrequently, we will defer
227      * this effort.
228      */
229 
230     /* Get version */
231     my_contextlist := xmldom.getElementsByTagName(my_doc, 'oa_context');
232     listlength := xmldom.getLength(my_contextlist);
233     if listlength < 1 then
234       -- may be it is metadata file
235       my_contextlist := xmldom.getElementsByTagName(my_doc,'oa_context_doc');
236       listlength := xmldom.getLength(my_contextlist);
237       if listlength < 1 then
238          element_name := 'oa_context/oa_context_doc';
239          dbms_lob.freetemporary(ctx_file_clob);
240          ctx_file_clob := null;
241          raise missing_elem;
242       end if;
243     end if;
244 
245     my_node := xmldom.item(my_contextlist, 0);
246     my_element := xmldom.makeElement(my_node);
247     file_ver := xmldom.getAttribute(my_element, 'version');
248 
249     file_ver := substr(file_ver, instr(file_ver,':',1,1) + 1);
250 
251     file_ver := rtrim(file_ver, '$');
252     file_ver := ltrim(rtrim(file_ver));
253   --  f_ver := to_number(NVL(substr(file_ver,instr(file_ver,'.',1,1) +1),0));
254 
255     /* Get Context name */
256     if ( file_type = 'TEMPLATE' ) then
257       context_name := 'TEMPLATE';
258     else
259       my_contextlist := xmldom.getElementsByTagName(my_doc, 'oa_context_name');
260       listlength := xmldom.getLength(my_contextlist);
261       if listlength < 1 then
262         context_name := 'METADATA';
263       else
264         my_node := xmldom.item(my_contextlist, 0);
265         my_node := xmldom.getFirstChild(my_node);
266         if xmldom.isNull(my_node) then
267            dbms_lob.freetemporary(ctx_file_clob);
268            ctx_file_clob := null;
269            raise null_context_name;
270         else
271            context_name := ltrim(rtrim(xmldom.getNodeValue(my_node)));
272         end if;
273       end if;
274     end if;
275 
276     if ( context_name in  ('METADATA', 'TEMPLATE') ) then
277        -- For metadata get node name from session
278        -- I don't think we need to handle no_data_found
279        select substr(machine, 1,
280 		     decode(instr(machine, '.', 1, 1),
281 				0, length(machine),
282 				instr(machine, '.', 1, 1)-1))
283 	 into primary_node
284          from v$session
285         where audsid=USERENV('SESSIONID');
286     else
287       /* Get Name of Node */
288       my_nodelist := xmldom.getElementsByTagName(my_doc, 'host');
289       listlength := xmldom.getLength(my_nodelist);
290       if listlength < 1 then
291         element_name := 'host';
292         dbms_lob.freetemporary(ctx_file_clob);
293         ctx_file_clob := null;
294         raise missing_elem;
295       end if;
296       my_node := xmldom.item(my_nodelist, 0);
297       my_node := xmldom.getFirstChild(my_node);
298       if xmldom.isNull(my_node) then
299          primary_node := null;
300       else
301          primary_node := ltrim(rtrim(xmldom.getNodeValue(my_node)));
302       end if;
303     end if;
304 
305     /* Get Serial Number from context file */
306     if ( context_name in ('METADATA', 'TEMPLATE')) then
307       sr_num := '0';
308     else
309       my_nodelist := xmldom.getElementsByTagName(my_doc, 'oa_context_serial');
310       listlength := xmldom.getLength(my_nodelist);
311       if listlength < 1 then
312         goto service_upld;
313       end if;
314       my_node := xmldom.item(my_nodelist, 0);
315       my_node := xmldom.getFirstChild(my_node);
316       if xmldom.isNull(my_node) then
317         sr_num := null;
318       else
319         sr_num := ltrim(rtrim(xmldom.getNodeValue(my_node)));
320       end if;
321     end if;
322 
323     f_sr_num := to_number(NVL(sr_num,0));
324 
325     -- try inserting row into fnd_oam_context_files table
326     -- fnd_oam_context_files.status values: (H)istory,(S)uccess,(F)ailure
327     --pbasha added for synchronization
328          if  (contextfile_handle is null)
329           then
330          dbms_lock.allocate_unique('contextfile_lock',contextfile_handle);
331           end if;
332     v_result := dbms_lock.request(lockhandle=>contextfile_handle,release_on_commit=>TRUE);
333     begin
334        select NVL(serial_number, 0), version into db_sr_num, db_ver_str
335          from fnd_oam_context_files
336         where (node_name = primary_node
337           and  path = filepath
338           and  name not in ('METADATA','TEMPLATE')
339           and  (status is null or upper(status) in ('S','F')))
340            or (path = filepath
341           and  name in ( 'METADATA', 'TEMPLATE'))
342           for update;
343 
344           file_found := TRUE;
345           v_result := dbms_lock.release(contextfile_handle);
346     exception
347        when no_data_found then
348           file_found := FALSE;
349     end;
350 
351 --    db_ver := to_number(NVL(substr(db_ver_str,instr(db_ver_str,'.',1,1) +1),0));
352 
353     if ( not file_found ) then
357                       last_updated_by, last_update_login, node_name,
354         insert into fnd_oam_context_files
355                      (name, version, path, last_synchronized,
356                       text, creation_date, created_by, last_update_date,
358                       status, ctx_type)
359               values (context_name, file_ver, filepath, sysdate,
360                       ctx_file_clob, sysdate, FND_GLOBAL.user_id, sysdate,
361                       FND_GLOBAL.user_id, FND_GLOBAL.login_id, primary_node,
362                       'S',
363                       DECODE(context_type, 'APPS', 'A', 'DATABASE','D','A'));
364        v_result := dbms_lock.release(contextfile_handle);
365 
366     else
367       -- overwrite the database copy of context file if the serial # of the
368       -- file being uploaded is >= to the copy in the DB.
369       -- or context version is > version in file(due to template patch)
370       -- for METADATA file we don't need to check for serial number.
371 
372          comp  := version_check(file_ver,db_ver_str);
373       if ( (( (f_sr_num >= db_sr_num) OR (comp > 0))
374               AND (context_name not in ('METADATA', 'TEMPLATE'))
375            OR context_name in ('METADATA', 'TEMPLATE'))) then
376 	 -- bug6739946
377 	 -- added status to be set to S to mark upload of template
378 	 -- to already existing row
379          update fnd_oam_context_files
380             set path = filepath,
381                 last_synchronized = sysdate,
382                 text = ctx_file_clob,
383                 last_update_date = sysdate,
384                 last_updated_by  = fnd_global.user_id,
385                 last_update_login = fnd_global.login_id,
386                 node_name = primary_node,
387                 version = file_ver,
388                 status = 'S'
389           where (node_name = primary_node
390             and  path = filepath
391             and  name not in ('METADATA', 'TEMPLATE')
392             and  (status is null or upper(status) in ('S','F')))
393              or (path = filepath
394             and  name in ( 'METADATA', 'TEMPLATE'));
395        else
396           raise filesys_low_ver;
397        end if;
398     end if;
399 
400     <<service_upld>>
401 
402     -- clear clob
403     dbms_lob.freetemporary(ctx_file_clob);
404     ctx_file_clob := null;
405     if ( context_name in ('METADATA', 'TEMPLATE')
406            or context_type = 'DATABASE') then
407        -- for metadata we don't need to register service definitions.
408        -- for database context file we don't need to register service def
409        raise metadata_file;
410     end if;
411 
412     -- if FND_DATABASES data model exists then try to insert topology info
413     if ( db_model_exists ) then
414     begin
415       sql_str := 'declare b boolean; Begin b := ' ||
416 		'fnd_conc_database.register_database(:1,:2,:3,:4,:5); ' ||
417 		' if (b) then :6 := 1; else :6 := 0; end if; end;';
418       select substr(value,1,8) into db_name
419         from v$parameter where name='db_name';
420       select substr(value,1,255) into db_domain
421         from v$parameter where name='db_domain';
422 
423       execute immediate sql_str using in db_name,
424                   in db_domain, in c_null, in c_null, in n_null, out ret_val;
425 
426 
427       /* Get db_host value */
428       my_nodelist := xmldom.getElementsByTagName(my_doc, 'dbhost');
429       listlength := xmldom.getLength(my_nodelist);
430       if listlength < 1 then
431         element_name := 'dbhost';
432         raise missing_elem;
433       end if;
434       my_node := xmldom.item(my_nodelist, 0);
435       my_node := xmldom.getFirstChild(my_node);
436       if xmldom.isNull(my_node) then
437         db_host := null;
438       else
439         db_host := ltrim(rtrim(xmldom.getNodeValue(my_node)));
440       end if;
441 
442       /* Get db_sid value */
443       my_nodelist := xmldom.getElementsByTagName(my_doc, 'dbsid');
444       listlength := xmldom.getLength(my_nodelist);
445       if listlength < 1 then
446         element_name := 'dbsid';
447         raise missing_elem;
448       end if;
449       my_node := xmldom.item(my_nodelist, 0);
450       my_node := xmldom.getFirstChild(my_node);
451       if xmldom.isNull(my_node) then
452         db_sid := null;
453       else
454         db_sid := ltrim(rtrim(xmldom.getNodeValue(my_node)));
455       end if;
456 
457       /* Get db_port value */
458       my_nodelist := xmldom.getElementsByTagName(my_doc, 'dbport');
459       listlength := xmldom.getLength(my_nodelist);
460       if listlength < 1 then
461         element_name := 'dbport';
462         raise missing_elem;
463       end if;
464       my_node := xmldom.item(my_nodelist, 0);
465       my_node := xmldom.getFirstChild(my_node);
466       if xmldom.isNull(my_node) then
467         db_port := null;
468       else
469         db_port := to_number(ltrim(rtrim(xmldom.getNodeValue(my_node))));
470       end if;
471 
472       select instance_number,instance_name
473         into inst_num, inst_name
474         from v$instance;
475 
476       sql_str := 'declare b boolean; Begin b := ' ||
477 		'fnd_conc_database.register_instance( ' ||
478                 ':1,:2,:3,:4,:5,:6,:7,:8,:9); ' ||
479 		' if (b) then :10 := 1; else :10 := 0; end if; end;';
480       execute immediate sql_str using in db_name,
481 		in inst_name, in inst_num, in db_sid, in db_host, in db_port,
482 		in db_sid, in c_null, in c_null, out ret_val;
483 
484 
485       exception
486         /* Ignore package(FND_CONC_DATABASE) not available cases. */
487         when packagenotexists then
488            null;
489       end;
490      end if;
491 
492 
496     listlength := xmldom.getLength(my_nodelist);
493 
494     /* Get Name of platform */
495     my_nodelist := xmldom.getElementsByTagName(my_doc, 'platform');
497     if listlength < 1 then
498       element_name := 'platform';
499       raise missing_elem;
500     end if;
501     my_node := xmldom.item(my_nodelist, 0);
502     my_node := xmldom.getFirstChild(my_node);
503     if xmldom.isNull(my_node) then
504       platform_name := null;
505     else
506       platform_name := ltrim(rtrim(xmldom.getNodeValue(my_node)));
507     end if;
508 
509 
510     -- Tru64 UNIX Alpha need hostname.domain_name as node name
511 
512     if ( platform_name = 'UNIX Alpha' ) then
513 	    /* Get Name of domain */
514         my_nodelist := xmldom.getElementsByTagName(my_doc, 'domain');
515         listlength := xmldom.getLength(my_nodelist);
516         if listlength < 1 then
517           element_name := 'domain';
518           raise missing_elem;
519         end if;
520         my_node := xmldom.item(my_nodelist, 0);
521         my_node := xmldom.getFirstChild(my_node);
522         if xmldom.isNull(my_node) then
523           domain_name := null;
524         else
525           domain_name := substr(ltrim(rtrim(xmldom.getNodeValue(my_node))), 1, 64);
526 	    end if;
527 
528         tmp_node_name := primary_node || '.' || domain_name;
529         if (length(tmp_node_name) > 30) then
530             raise exceeded_length;
531         else
532             primary_node := tmp_node_name;
533         end if;
534     end if;
535 
536     /* Since port info is stored separately from the process info,
537        we first retrieve the ports, and then loop through the processes
538        to register the services. */
539 
540 
541     /* Get the Reports port */
542 
543     my_nodelist := xmldom.getElementsByTagName(my_doc, 'reports_port');
544     listlength := xmldom.getLength(my_nodelist);
545     if listlength < 1 then
546       element_name := 'reports_port';
547       -- To fix 4684481
548       -- raise missing_elem;
549     else
550       my_node := xmldom.item(my_nodelist, 0);
551       my_node := xmldom.getFirstChild(my_node);
552       if xmldom.isNull(my_node) then
553         reports_port := null;
554       else
555         reports_port := ltrim(rtrim(xmldom.getNodeValue(my_node)));
556       end if;
557     end if;
558 
559 
560 
561     /* Get the Forms port */
562 
563     my_nodelist := xmldom.getElementsByTagName(my_doc, 'forms_port');
564     listlength := xmldom.getLength(my_nodelist);
565     if listlength < 1 then
566       element_name := 'forms_port';
567       -- To fix bug 5151130
568       -- raise missing_elem;
569     end if;
570     my_node := xmldom.item(my_nodelist, 0);
571     my_node := xmldom.getFirstChild(my_node);
572     if xmldom.isNull(my_node) then
573       forms_port := null;
574     else
575       forms_port := ltrim(rtrim(xmldom.getNodeValue(my_node)));
576     end if;
577 
578     /* Get the Metrics Server Node */
579 
580     methost_node := null;
581 
582     my_nodelist := xmldom.getElementsByTagName(my_doc, 'hostname');
583     listlength := xmldom.getLength(my_nodelist);
584 
585     if listlength > 1 then
586       for i in 0..listlength-1 loop
587         my_node := xmldom.item(my_nodelist, i);
588 	my_element := xmldom.makeElement(my_node);
589 	oh_type := xmldom.getAttribute(my_element, 'oa_var');
590 	exit when oh_type = 's_methost';
591       end loop;
592     end if;
593 
594     if (oh_type = 's_methost') then
595       my_node := xmldom.getFirstChild(my_node);
596       if not xmldom.isNull(my_node) then
597         methost_node := ltrim(rtrim(xmldom.getNodeValue(my_node)));
598       end if;
599     end if;
600     if methost_node is null then
601       methost_node := primary_node;
602     end if;
603 
604     /* Get the metrics request port */
605 
606     my_nodelist := xmldom.getElementsByTagName(my_doc, 'met_req_port');
607     listlength := xmldom.getLength(my_nodelist);
608     if listlength < 1 then
609       element_name := 'met_req_port';
610       -- To fix bug 5151130
611       -- raise missing_elem;
612     else
613       my_node := xmldom.item(my_nodelist, 0);
614       my_node := xmldom.getFirstChild(my_node);
615       if xmldom.isNull(my_node) then
616         met_req_port := null;
617       else
618         met_req_port := ltrim(rtrim(xmldom.getNodeValue(my_node)));
619       end if;
620     end if;
621 
622 
623     /* Get the metrics data port */
624 
625     my_nodelist := xmldom.getElementsByTagName(my_doc, 'met_data_port');
626     listlength := xmldom.getLength(my_nodelist);
627     if listlength < 1 then
628       element_name := 'met_data_port';
629       -- To fix bug 5151130
630       -- raise missing_elem;
631     else
632       my_node := xmldom.item(my_nodelist, 0);
633       my_node := xmldom.getFirstChild(my_node);
634       if xmldom.isNull(my_node) then
635         met_data_port := null;
636       else
637         met_data_port := ltrim(rtrim(xmldom.getNodeValue(my_node)));
638       end if;
639     end if;
640 
641     /* Get the web port */
642 
643     my_nodelist := xmldom.getElementsByTagName(my_doc, 'web_port');
644     listlength := xmldom.getLength(my_nodelist);
645     if listlength < 1 then
646       element_name := 'web_port';
647       raise missing_elem;
648     end if;
649     my_node := xmldom.item(my_nodelist, 0);
650     my_node := xmldom.getFirstChild(my_node);
651     if xmldom.isNull(my_node) then
652       web_port := null;
656 
653     else
654       web_port := ltrim(rtrim(xmldom.getNodeValue(my_node)));
655     end if;
657     /* Get web pls port if any */
658 
659     my_nodelist := xmldom.getElementsByTagName(my_doc, 'web_port_pls');
660     listlength := xmldom.getLength(my_nodelist);
661     if listlength > 0 then
662        my_node := xmldom.item(my_nodelist, 0);
663        my_node := xmldom.getFirstChild(my_node);
664        if xmldom.isNull(my_node) then
665          web_pls_port := null;
666        else
667          web_pls_port := ltrim(rtrim(xmldom.getNodeValue(my_node)));
668        end if;
669     else
670        web_pls_port := null;
671     end if;
672 
673 
674     /* Get the Apache_Top */
675 
676     my_nodelist := xmldom.getElementsByTagName(my_doc, 'ORACLE_HOME');
677     listlength := xmldom.getLength(my_nodelist);
678     if listlength < 1 then
679       element_name := 'ORACLE_HOME oa_var=s_web_oh';
680       raise missing_elem;
681     end if;
682 
683     for i in 0..listlength-1 loop
684       my_node := xmldom.item(my_nodelist, i);
685       my_element := xmldom.makeElement(my_node);
686       oh_type := xmldom.getAttribute(my_element, 'oa_var');
687       exit when oh_type = 's_weboh_oh';
688     end loop;
689 
690     if (oh_type = 's_weboh_oh') then
691         my_node := xmldom.getFirstChild(my_node);
692         if xmldom.isNull(my_node) then
693           apache_top := null;
694         else
695           apache_top := ltrim(rtrim(xmldom.getNodeValue(my_node)));
696         end if;
697     else
698       element_name := 'ORACLE_HOME oa_var=s_web_oh';
699       raise missing_elem;
700     end if;
701 
702 
703     /* Get the web pid file if any, do not show this an error */
704 
705     my_nodelist := xmldom.getElementsByTagName(my_doc, 'web_pid_file');
706     listlength := xmldom.getLength(my_nodelist);
707     if listlength > 0 then
708        my_node := xmldom.item(my_nodelist, 0);
709        my_node := xmldom.getFirstChild(my_node);
710        if xmldom.isNull(my_node) then
711          web_pid_file := null;
712        else
713          web_pid_file := ltrim(rtrim(xmldom.getNodeValue(my_node)));
714        end if;
715     end if;
716 
717     /* bug11844982                                                */
718     /* Get the web pls pid file if any, do not show this an error */
719 
720     web_pls_pid_file := null;
721 
722     my_nodelist := xmldom.getElementsByTagName(my_doc, 'httpd_pls_pid_file');
723     listlength := xmldom.getLength(my_nodelist);
724     if listlength > 0 then
725       my_node := xmldom.item(my_nodelist, 0);
726       my_node := xmldom.getFirstChild(my_node);
727       if not xmldom.isNull(my_node) then
728         web_pls_pid_file := ltrim(rtrim(xmldom.getNodeValue(my_node)));
729       end if;
730     end if;
731     if web_pls_pid_file is null then
732       web_pls_pid_file := apache_top||'/Apache/Apache/logs/httpd_pls.pid';
733     end if;
734 
735     /* Get logs_dir value, used for reports */
736     my_nodelist := xmldom.getElementsByTagName(my_doc, 'logs_dir');
737     listlength := xmldom.getLength(my_nodelist);
738     if listlength > 0 then
739        my_node := xmldom.item(my_nodelist, 0);
740        my_node := xmldom.getFirstChild(my_node);
741        if xmldom.isNull(my_node) then
742          logs_dir := null;
743        else
744          logs_dir := ltrim(rtrim(xmldom.getNodeValue(my_node)));
745        end if;
746     else
747        element_name := 'logs_dir oa_var=s_logdir';
748        raise missing_elem;
749     end if;
750 
751 
752     /* Get techstack value */
753     my_nodelist := xmldom.getElementsByTagName(my_doc, 'config_option');
754     listlength := xmldom.getLength(my_nodelist);
755     if listlength < 1 then
756        tech_stack := null;
757     end if;
758 
759     for i in 0..listlength-1 loop
760       my_node := xmldom.item(my_nodelist, i);
761       my_element := xmldom.makeElement(my_node);
762       oh_type := xmldom.getAttribute(my_element, 'oa_var');
763       exit when oh_type = 's_techstack';
764     end loop;
765 
766     if (oh_type = 's_techstack') then
767         my_node := xmldom.getFirstChild(my_node);
768         if xmldom.isNull(my_node) then
769           tech_stack := null;
770         else
771           tech_stack := ltrim(rtrim(xmldom.getNodeValue(my_node)));
772         end if;
773     else
774         tech_stack := null;
775     end if;
776 
777 
778     /* find out mod_pls process is exists or not. If exists then pass
779        'core' to Apache control script, else old stuff.
780      */
781 
782     mod_pls_exists := FALSE;
783     my_nodelist := xmldom.getElementsByTagname(my_doc, 'oa_process');
784     listlength := xmldom.getLength(my_nodelist);
785     for i in 0..(listlength -1 ) loop
786       my_node := xmldom.item(my_nodelist, i);
787       my_element := xmldom.makeElement(my_node);
788       server_type := xmldom.getAttribute(my_element, 'type');
789 
790       if (server_type = 'apache_pls' ) then
791          mod_pls_exists := TRUE;
792       end if;
793     end loop;
794 
795 
796     /* Register Services */
797 
798     my_nodelist := xmldom.getElementsByTagName(my_doc, 'oa_process');
799     listlength := xmldom.getLength(my_nodelist);
800     for i in 0..(listlength - 1) loop
801       my_node := xmldom.item(my_nodelist, i);
802       my_element := xmldom.makeElement(my_node);
803       server_type := xmldom.getAttribute(my_element, 'type');
804 
805 
806       /* Get the process name */
807       my_nodelist2 := xmldom.getElementsByTagName(my_element,
811         element_name := 'oa_process_name';
808                                                       'oa_process_name');
809       listlength := xmldom.getLength(my_nodelist2);
810       if listlength < 1 then
812         raise missing_elem;
813       end if;
814       my_node2 := xmldom.item(my_nodelist2, 0);
815       my_node2 := xmldom.getFirstChild(my_node2);
816       if xmldom.isNull(my_node2) then
817         process_name := null;
818       else
819         process_name := ltrim(rtrim(
820                             xmldom.getNodeValue(my_node2), '" '), '" ');
821       end if;
822 
823 
824       /* Get the control script name. */
825       my_nodelist2 := xmldom.getElementsByTagName(my_element,
826                                                  'ctrl_script');
827       listlength := xmldom.getLength(my_nodelist2);
828       if listlength < 1 then
829         element_name := 'ctrl_script';
830         raise missing_elem;
831       end if;
832       my_node2 := xmldom.item(my_nodelist2, 0);
833       my_node2 := xmldom.getFirstChild(my_node2);
834       if xmldom.isNull(my_node2) then
835         ctrl_script := null;
836       else
837         ctrl_script := ltrim(rtrim(
838                              xmldom.getNodeValue(my_node2), '" '), '" ');
839       end if;
840 
841 
842       /* Get the log file name name. */
843       my_nodelist2 := xmldom.getElementsByTagName(my_element,
844                                                  'oa_process_log');
845       listlength := xmldom.getLength(my_nodelist2);
846       if listlength < 1 then
847         element_name := 'oa_process_log';
848         raise missing_elem;
849       end if;
850       my_node2 := xmldom.item(my_nodelist2, 0);
851       my_node2 := xmldom.getFirstChild(my_node2);
852       if xmldom.isNull(my_node2) then
853         process_log := null;
854       else
855         process_log := ltrim(rtrim(
856                              xmldom.getNodeValue(my_node2), '" '), '" ');
857       end if;
858 
859 
860       if server_type in ('apache','forms','met_cl','met_srv',
861                          'reports', 'apache_pls') then
862 
863         /* Determine full name, port, and short name */
864         if server_type  = 'apache' then
865           message_name := 'GSM-APACHE SVC INST';
866           port := web_port;
867           short_name := 'APACHE_'||substrb(primary_node, 1, 12)||'_'||
868                         substr(port, 1, 8);
869           service_handle := 'Apache';
870 
871           /* if mod_pls_exists then pass 'core' to regular apache */
872           if (mod_pls_exists) then
873              parameters :=  'START=PATH,'|| ctrl_script||' start core;'||
874                          'STOP=PATH,'||ctrl_script||' stop core;'||
875                          'LOG='||process_log||';';
876           else
877              parameters := 'START=PATH,'|| ctrl_script||' start;'||
878                            'STOP=PATH,'||ctrl_script||' stop;'||
879                            'LOG='||process_log||';';
880           end if;
881 
882           /* determine pid file for ias10 */
883           if ( tech_stack = 'ias10' ) then
884               if ( substr(upper(platform_name),
885                            length(platform_name)-1) <> 'NT' ) then
886 		web_pid_file := apache_top || '/Apache/Apache/logs/httpds.pid';
887               else
888                 web_pid_file := apache_top || '/Apache/Apache/logs/httpd.pid';
889               end if;
890           end if;
891 
892           if (apache_top is not null) then
893             /* if pid file is there then use it */
894             if ( web_pid_file is not null) then
895               parameters := parameters || 'PID=FILE,PATH,' ||web_pid_file||
896                             ';'|| 'SERVICE='||process_name;
897             else
898               parameters := parameters || 'PID=FILE,PATH,'||apache_top||
899                            '/Apache/Apache/logs/httpds.pid;'||
900                            'SERVICE='||process_name;
901             end if;
902           else
903             parameters := parameters || 'SERVICE='||process_name;
904           end if;
905 
906         elsif server_type  = 'apache_pls' then
907           message_name := 'GSM-MOD PLS SVC INST';
908           port := web_pls_port;
909           short_name := 'APACHE_'||substrb(primary_node, 1, 12)||'_'||
910                         substr(port, 1, 8);
911           service_handle := 'Apache';
912 
913           parameters :=  'START=PATH,'|| ctrl_script||' start pls;'||
914                          'STOP=PATH,'||ctrl_script||' stop pls;'||
915                          'LOG='||process_log||';';
916 
917           /* designate the pls pid file */
918           if (apache_top is not null) then
919               parameters := parameters || 'PID=FILE,PATH,' ||web_pls_pid_file||
920                             ';'|| 'SERVICE='||process_name;
921           else
922             parameters := parameters || 'SERVICE='||process_name;
923           end if;
924 
925           /* The PID file won't be used on NT, so the Unix style
926              directory delimiters above should not be a problem. */
927 
928 
929         elsif server_type  = 'forms' then
930           message_name := 'GSM-FORMS SVC INST';
931           port := forms_port;
932           short_name := 'FORMS_'||substr(primary_node, 1, 12)||'_'||
933                          substrb(port, 1, 8);
934           service_handle := 'FormsL';
935           parameters :=  'START=PATH,'|| ctrl_script||' start;'||
936                          'STOP=PATH,'||ctrl_script||' stop;'||
937                          'LOG='||process_log||';'||
938                          'PID=FIND,f60srvm,'||primary_node||'_'||port||';'||
939                          'SERVICE='||process_name;
943           message_name := 'GSM-METRICS CL SVC INST';
940 
941 
942         elsif server_type  = 'met_cl' then
944           port := met_data_port;
945           short_name := 'MET_CL_'||substr(primary_node, 1, 12)||'_'||
946                          substrb(met_data_port, 1, 8);
947           service_handle := 'FormsMC';
948           parameters :=  'START=PATH,'|| ctrl_script||' start;'||
949                          'STOP=PATH,'||ctrl_script||' stop;'||
950                          'LOG='||process_log||';'||
951                          'PID=FIND,d2lc60,'||methost_node||
952                          ','||met_data_port||';'||
953                          'SERVICE='||process_name;
954 
955         elsif server_type  = 'met_srv' then
956           message_name := 'GSM-METRICS SRV SVC INST';
957           port := met_req_port;
958           short_name := 'MET_SRV_'||substr(primary_node, 1, 12)||'_'||
959                          substrb(met_req_port, 1, 8);
960           service_handle := 'FormsMS';
961           parameters :=  'START=PATH,'|| ctrl_script||' start;'||
962                          'STOP=PATH,'||ctrl_script||' stop;'||
963                          'LOG='||process_log||';'||
964                          'PID=FIND,d2ls60,'||met_data_port||
965                          ','||met_req_port||';'||
966                          'SERVICE='||process_name;
967 
968         elsif server_type  = 'reports' then
969           /* Note the service name has the format:
970            *    "Oracle Reports Server [short_name]"
971            * Here we extract the short_name.                     */
972           reports_short_name := rtrim(substr(process_name,
973                                        instr(process_name,'[')+1), '] ');
974           /* replace Rep60 with REP60
975            * using parse mechanism instead of replace func
976            */
977           reports_short_name := upper(substr(reports_short_name, 1,
978                                        instr(reports_short_name,'_'))) ||
979 				substr(reports_short_name,
980 					instr(reports_short_name,'_')+1);
981 
982 	  /* 3578632 adrepctl now uses REP60_%s_dbSid% as the REPSERV_NAME,
983            *  instead of s_reptname as we do here.                          */
984 	  if (db_sid is not null ) then
985 		reports_short_name := 'REP60_' || db_sid;
986 	  end if;
987 
988           message_name := 'GSM-REPORTS SRV SVC INST';
989           port := reports_port;
990           short_name := 'REPORTS_'||substrb(primary_node, 1, 12)||'_'||
991                         substr(port, 1, 8);
992           service_handle := 'RepServ';
993 
994           parameters :=  'START=PATH,'|| ctrl_script||' start;'||
995                          'STOP=PATH,'||ctrl_script||' stop;'||
996                          'LOG='||process_log||';'||
997                          'PID=FILE,PATH,'||logs_dir||'/'||reports_short_name||
998 			 '.PID'||';'||'SERVICE='||process_name;
999        end if;
1000 
1001         /* Is the service enabled? */
1002         my_nodelist2 := xmldom.getElementsByTagName(my_element,
1003                                                     'oa_process_status');
1004         listlength := xmldom.getLength(my_nodelist2);
1005         if listlength < 1 then
1006           element_name := 'oa_process_status';
1007           raise missing_elem;
1008         end if;
1009         my_node2 := xmldom.item(my_nodelist2, 0);
1010         my_node2 := xmldom.getFirstChild(my_node2);
1011         if xmldom.isNull(my_node2) then
1012           status := 'disabled';
1013         else
1014           status := ltrim(rtrim(xmldom.getNodeValue(my_node2), '" '), '" ');
1015         end if;
1016 
1017         if status = 'enabled' then
1018           /* Does the service exist in GSM? */
1019           begin
1020             select concurrent_queue_id, application_id, enabled_flag
1021               into queue_id, queue_appl_id, fcq_enabled_flag
1022               from fnd_concurrent_queues
1023              where application_id = 0
1024                and concurrent_queue_name = short_name;
1025 
1026             /* Yes, the service exists.  Otherwise, there would *
1027              * have been a no_data_found exception.             */
1028 
1029 
1030             /* Update the workshift parameters. */
1031 
1032             update fnd_concurrent_queue_size
1033                set service_parameters = parameters
1034              where concurrent_queue_id = queue_id
1035                and queue_application_id = queue_appl_id;
1036 
1037 
1038           exception
1039 
1040             when no_data_found then
1041               /* The service is not registered with GSM */
1042               /* Do the registration now. */
1043               fnd_message.set_name('FND', message_name);
1044               fnd_message.set_token('NODE', primary_node);
1045               fnd_message.set_token('PORT', port);
1046               service_name := substrb(fnd_message.get, 1, 240);
1047 
1048               begin
1049                 fnd_manager.register_si(manager => service_name,
1050                                         application => 'FND',
1051                                         short_name => short_name,
1052                                         service_handle => service_handle,
1053                                         primary_node => upper(primary_node),
1054                                         language_code => userenv('LANG'));
1055 
1056 
1057                 fnd_manager.assign_work_shift(
1058                                         manager_short_name => short_name,
1059                                         manager_application => 'FND',
1060                                         processes => 1,
1061                                         sleep_seconds => 1,
1062                                         work_shift_id => 0,
1063                                         svc_params => parameters);
1064 
1068                   from fnd_concurrent_queues
1065                  /* Now update enabled flag to 'N' */
1066                 select concurrent_queue_id, application_id, enabled_flag
1067                   into queue_id, queue_appl_id, fcq_enabled_flag
1069                  where application_id = 0
1070                    and concurrent_queue_name = short_name;
1071 
1072                 update fnd_concurrent_queues
1073                    set enabled_flag = 'N'
1074                  where concurrent_queue_id = queue_id
1075                    and application_id = queue_appl_id;
1076 
1077               exception
1078                 when program_error then
1079                   message := fnd_manager.message;
1080 
1081                   if (not xmldom.isNull(my_doc)) then
1082                      xmldom.freeDocument (my_doc);
1083                   end if;
1084                   xmlparser.freeParser (my_parser);
1085 
1086                   retcode := 1;
1087                   return;
1088 
1089                 when others then
1090                   fnd_message.set_name('FND', 'SQL_PLSQL_ERROR');
1091                   fnd_message.set_token('ROUTINE',
1092                                         'FND_GSM_UTIL.upload_context_file');
1093                   fnd_message.set_token('ERRNO', SQLCODE);
1094                   fnd_message.set_token('REASON', SQLERRM);
1095                   message :=  fnd_message.get;
1096 
1097                   if (not xmldom.isNull(my_doc)) then
1098                      xmldom.freeDocument (my_doc);
1099                   end if;
1100                   xmlparser.freeParser (my_parser);
1101 
1102                   retcode := 1;
1103                   return;
1104               end;
1105           end;
1106 
1107         else /* Service is disabled in context file. */
1108 
1109           /* Update fcq if necessary */
1110           update fnd_concurrent_queues
1111              set enabled_flag = 'N'
1112            where application_id = 0
1113              and concurrent_queue_name = short_name;
1114 
1115         end if;
1116 
1117       end if;
1118 
1119     end loop;
1120 
1121     if (not xmldom.isNull(my_doc)) then
1122        xmldom.freeDocument (my_doc);
1123     end if;
1124 
1125     xmlparser.freeParser (my_parser);
1126 
1127     retcode := 0;
1128 
1129   exception
1130     when metadata_file then
1131       if (not xmldom.isNull(my_doc)) then
1132          xmldom.freeDocument (my_doc);
1133       end if;
1134       xmlparser.freeParser (my_parser);
1135 
1136       retcode := 0;
1137 
1138     when null_context_name then
1139       fnd_message.set_name('FND', 'GSM-CTX NULL CONTEXT');
1140       fnd_message.set_token('FILE', filepath);
1141       message := fnd_message.get;
1142 
1143       if (not xmldom.isNull(my_doc)) then
1144          xmldom.freeDocument (my_doc);
1145       end if;
1146       xmlparser.freeParser (my_parser);
1147 
1148       retcode := 1;
1149 
1150     when filesys_low_ver then
1151       fnd_message.set_name('FND', 'GSM-CTX FILE SYS COPY LOW');
1152       fnd_message.set_token('FILE', filepath);
1153       message := fnd_message.get;
1154 
1155       if (not xmldom.isNull(my_doc)) then
1156          xmldom.freeDocument (my_doc);
1157       end if;
1158       xmlparser.freeParser (my_parser);
1159 
1160       retcode := 1;
1161 
1162     when missing_elem then
1163       fnd_message.set_name('FND', 'GSM-CTX ELEMENT MISSING');
1164       fnd_message.set_token('ELEMENT', element_name);
1165       fnd_message.set_token('FILE', filepath);
1166       message :=  fnd_message.get;
1167 
1168       if (not xmldom.isNull(my_doc)) then
1169          xmldom.freeDocument (my_doc);
1170       end if;
1171       xmlparser.freeParser (my_parser);
1172 
1173       retcode := 1;
1174 
1175     when exceeded_length then
1176       fnd_message.set_name('FND', 'GSM-CTX NODE NAME TOO LONG');
1177       fnd_message.set_token('NODE_NAME', tmp_node_name);
1178       message := fnd_message.get;
1179 
1180       if (not xmldom.isNull(my_doc)) then
1181          xmldom.freeDocument (my_doc);
1182       end if;
1183       xmlparser.freeParser (my_parser);
1184 
1185       retcode := 1;
1186 
1187     when others then
1188       fnd_message.set_name('FND', 'SQL_PLSQL_ERROR');
1189       fnd_message.set_token('ROUTINE', 'FND_GSM_UTIL.upload_context_file');
1190       fnd_message.set_token('ERRNO', SQLCODE);
1191       fnd_message.set_token('REASON', SQLERRM);
1192       message :=  fnd_message.get;
1193 
1194       if (not xmldom.isNull(my_doc)) then
1195          xmldom.freeDocument (my_doc);
1196       end if;
1197       xmlparser.freeParser (my_parser);
1198 
1199       retcode := 1;
1200 
1201   end upload_context_file;
1202 
1203 function version_check(version1 varchar,version2 varchar) return number is
1204          l_version1      varchar2(500) default version1 || '.';
1205 	 l_version2      varchar2(500) default version2 || '.';
1206 	 TYPE data IS TABLE OF varchar2(100);
1207          l_data1  data := data();
1208 	 l_data2  data := data();
1209 	 n number;
1210 	 m number;
1211 	 x number;
1212 	 p number;
1213 
1214   begin
1215 
1216       loop
1217            exit when l_version1 is null and l_version2 is null;
1218 	   if l_version1 is not null then
1219               n := instr( l_version1, '.' );
1220               l_data1.extend;
1221               l_data1(l_data1.count) := ltrim( rtrim( substr( l_version1, 1, n-1 ) ) );
1222               l_version1 := substr( l_version1, n+1 );
1223 	   end if;
1224 	   if l_version2 is not null then
1225 	      m := instr( l_version2, '.' );
1226               l_data2.extend;
1227               l_data2(l_data2.count) := ltrim( rtrim( substr( l_version2, 1, m-1 ) ) );
1228               l_version2 := substr( l_version2, m+1 );
1229 	    end if;
1230 
1231      end loop;
1232 
1233          if (l_data1.count < l_data2.count) then
1234                 x := l_data1.count;
1235           else
1236 	        x := l_data2.count;
1237            end if;
1238      for i in 1 .. x
1239         loop
1240           p:=  to_number(NVL(l_data1(i),0))-to_number(NVL(l_data2(i),0));
1241 	  if (p <> 0) then
1242 	      return p;
1243 	  end if;
1244        end loop;
1245          return l_data1.count - l_data2.count;
1246   end version_check;
1247 end fnd_gsm_util;