|
1 /* |
|
2 * Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the License "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 package com.nokia.helium.metadata.ant.taskdefs; |
|
19 |
|
20 import com.nokia.helium.metadata.CustomMetaDataProvider; |
|
21 import com.nokia.helium.metadata.MetaDataInput; |
|
22 import com.nokia.helium.jpa.entity.metadata.Metadata; |
|
23 import org.apache.tools.ant.BuildException; |
|
24 import org.apache.tools.ant.Task; |
|
25 import java.util.Vector; |
|
26 import java.util.Iterator; |
|
27 import org.apache.log4j.Logger; |
|
28 import com.nokia.helium.metadata.db.*; |
|
29 import java.util.Date; |
|
30 |
|
31 /** |
|
32 * This task provide a way to record the data in the Database. |
|
33 * |
|
34 * <pre> |
|
35 * Example 1: |
|
36 * <metadatarecord database="compile_log.db"> |
|
37 * <sbsmetadatainput> |
|
38 * <fileset casesensitive="false" file="sbs.log.file"/> |
|
39 * <metadatafiltelistref refid="compilation"/> |
|
40 * </sbsmetadatainput> |
|
41 * </metadatarecord> |
|
42 * |
|
43 * Example 2: |
|
44 * |
|
45 * <metadatarecord database="metadata.db"> |
|
46 * <antmetadatainput> |
|
47 * <fileset casesensitive="false" file="${build.id}_ant_build.log"/> |
|
48 * <metadatafiltelistref refid="compilation"/> |
|
49 * </antmetadatainput> |
|
50 * </metadatarecord> |
|
51 |
|
52 * </pre> |
|
53 * |
|
54 * @ant.task name="metadatarecord" category="Metadata" |
|
55 */ |
|
56 public class MetaDataRecord extends Task { |
|
57 |
|
58 private static Logger log = Logger.getLogger(MetaDataRecord.class); |
|
59 |
|
60 private String database; |
|
61 |
|
62 private boolean failOnError = true; |
|
63 |
|
64 private Vector<MetaDataInput> metadataList = new Vector<MetaDataInput>(); |
|
65 |
|
66 /** |
|
67 * Helper function to set the database parameter |
|
68 * |
|
69 * @ant.required |
|
70 */ |
|
71 public void setDatabase(String dbFile) { |
|
72 database = dbFile; |
|
73 } |
|
74 |
|
75 public void setFailOnError(String failNotify) { |
|
76 if (failNotify.equals("false")) { |
|
77 failOnError = false; |
|
78 } |
|
79 } |
|
80 /** |
|
81 * Helper function to get the database |
|
82 * |
|
83 */ |
|
84 public String getDatabase() { |
|
85 return database; |
|
86 } |
|
87 |
|
88 /** |
|
89 * Helper function to return the metadatalist |
|
90 * @return build metadata object |
|
91 * |
|
92 */ |
|
93 public Vector<MetaDataInput> getMetaDataList() throws Exception { |
|
94 if (metadataList.isEmpty()) { |
|
95 throw new Exception("metadata list is empty"); |
|
96 } |
|
97 return metadataList; |
|
98 } |
|
99 |
|
100 /** |
|
101 * Helper function to add the metadatalist |
|
102 * @param build metadata list to add |
|
103 * |
|
104 */ |
|
105 public void add(MetaDataInput interf) { |
|
106 metadataList.add(interf); |
|
107 } |
|
108 |
|
109 |
|
110 @Override |
|
111 public void execute() { |
|
112 ORMMetadataDB ormDB = null; |
|
113 try { |
|
114 log.debug("Getting Contents to write to db: " + database); |
|
115 log.debug("Initializing DB: " + database); |
|
116 log.debug("initializing ORM db"); |
|
117 ormDB = new ORMMetadataDB(database); |
|
118 log.debug("Parsing the input and writing to DB"); |
|
119 Date before = new Date(); |
|
120 log("Time before recording to db: " + before); |
|
121 for (MetaDataInput metadataInput : metadataList) { |
|
122 boolean removed = false; |
|
123 String logPath = null; |
|
124 Iterator<Metadata.LogEntry> inputIterator = metadataInput.iterator(); |
|
125 while (inputIterator.hasNext()) { |
|
126 Metadata.LogEntry logEntry = inputIterator.next(); |
|
127 if (!removed) { |
|
128 logPath = logEntry.getLogPath(); |
|
129 ormDB.removeEntries(logPath); |
|
130 removed = true; |
|
131 } |
|
132 //initializes the metadata if none exists |
|
133 ormDB.addLogEntry(logEntry); |
|
134 } |
|
135 if (logPath != null && metadataInput instanceof CustomMetaDataProvider) { |
|
136 CustomMetaDataProvider provider = (CustomMetaDataProvider)metadataInput; |
|
137 provider.provide(ormDB, logPath); |
|
138 } |
|
139 if (logPath != null) { |
|
140 ormDB.finalizeMetadata(logPath); |
|
141 } |
|
142 } |
|
143 Date after = new Date(); |
|
144 log("Time after recording to db: " + after); |
|
145 log("Elapsed time: " + (after.getTime() - before.getTime()) + " ms"); |
|
146 log.debug("Successfully writen to DB"); |
|
147 } catch (BuildException ex1) { |
|
148 log.debug("BuildException during writing to db: ", ex1); |
|
149 if (failOnError) { |
|
150 throw ex1; |
|
151 } |
|
152 } finally { |
|
153 if (ormDB != null) { |
|
154 ormDB.finalizeDB(); |
|
155 } |
|
156 } |
|
157 } |
|
158 } |