I'm developing a maven-plugin to check dependencies' licenses, similar to this one, but I can't found any API for that... what I would like to have is something like
String licence = artifact.getLicence();
mrice on that license-check plugin just try to find the .pom file of that artifact, read it, and try to find the <license></license>.
Do you know how can I do this?
You need to use org.apache.maven.model.License. (See api docs for details).
Something like this:
try {
Reader reader = new FileReader(pomXmlFile);
Model model;
try {
final MavenXpp3Reader xpp3Reader = new MavenXpp3Reader();
model = xpp3Reader.read(reader);
} finally {
reader.close();
}
List<License> licenses = model.getLicenses();
} catch (XmlPullParserException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
*Note that many poms do not have a license.
Related
I am trying to get the google ad id (GAID)in android java but I am getting exeception like:
androidx.ads.identifier.AdvertisingIdNotAvailableException: No compatible AndroidX Advertising ID Provider available.
My Code
ListenableFuture<AdvertisingIdInfo> adInfo = AdvertisingIdClient.getAdvertisingIdInfo(getApplicationContext());
System.out.println("adinfo"+adInfo.toString());
try {
String id = adInfo.get().getId();
System.out.println("adod"+id);
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
And dependienced i added
implementation 'com.google.android.gms:play-services-ads:19.4.0'
implementation 'androidx.ads:ads-identifier:1.0.0-alpha04'
In Manifest I added
<meta-data
android:name="com.google.android.gms.ads.AD_MANAGER_APP"
android:value="true"/>
I have tried many solutions but it is not useful. Please help me out to solve the issue.
The gaid dependency in androidx seems to be at fault.
Many people (including me) have followed Google's ad ID documentation and have reported the same issue. Also, Google's documentation on this matter appears outdated since the examplatory code snippet is wrong (e.g. function addCallback is missing a parameter).
The solution is to add this dependency in your gradle file instead of the ones you mentioned:
implementation 'com.google.android.gms:play-services-ads-identifier:17.0.0'
You can then fetch the advertising id this way:
AdvertisingIdClient.Info idInfo;
try {
idInfo = AdvertisingIdClient.getAdvertisingIdInfo(context);
String advertisingId = idInfo.getId();
//do sth with the id
} catch (IOException e) {
e.printStackTrace();
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
} catch (GooglePlayServicesRepairableException e) {
e.printStackTrace();
}
If you want to use the id only when the user hasn't opted out of personalised ads, you can add this after retrieving the id info:
//if user hasn't opted out of ads in device settings
if(idInfo.isLimitAdTrackingEnabled()) {
//do sth with the id
}
Edit: The mentioned Google's documentation actually has a note that encourages you to use the ads identifier library I mentioned:
I use the following code to filter out javascript code in user-submitted html files on Android:
Policy antiSamyPolicy;
try {
antiSamyPolicy = Policy.getInstance(AntiSamy.class.getResourceAsStream("/antisamy.xml"));
} catch (PolicyException e) {
e.printStackTrace();
return;
}
AntiSamy antiSamy = new AntiSamy(antiSamyPolicy);
CleanResults result;
try {
result = antiSamy.scan(taintedHtml);
} catch (PolicyException | ScanException e) {
e.printStackTrace();
return;
}
It loads bundled policy "antisamy.xml" which is included in AntiSamy (https://github.com/nahsra/antisamy).
All seems to work ok. The only question is how actual is the policy? Is it enough to filter out all javascript code in contemporary html?
I try to load a property file in Java running on JBossFuse/karaf.
The file is located at $[karaf.home]/etc/bean.properties
The Code is able to load properties inside the bundle fine, but now I try to exclude the properties from the project itself and the code throws a Nullpointer-Exception.
The Path is properly resolved on my development machine as
C:\Users\someone\devstudio\runtimes\jboss-fuse-6.3.0.redhat-135\etc\bean.properties
The property-File can be loaded in the blueprint-XML to configure beans, but to access the bean my code needs the CamelContext. As I have some static codeblocks that are accessed without an exchange/context/registry, I also wanted to be able to load the properties in Java.
Both the functions throw the NullPointerException and I guess, it is because the code runs in Fuse.
public static Properties getProperties(String location) {
Properties prop = new Properties();
InputStream input = null;
try {
input = PropertyLoader.class.getClassLoader().getResourceAsStream(location);
prop.load(input);
} catch (IOException ex) {
log.error("Error loading properties file from: " + location, ex);
return null;
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
log.error(e);
}
}
}
return prop;
}
public static Properties getPropertiesFromFilesystem(String location) {
Properties prop = new Properties();
InputStream input = null;
try {
input = new FileInputStream(location);
prop.load(input);
} catch (IOException ex) {
log.error("Error loading properties file from: " + location, ex);
return null;
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
log.error(e);
}
}
}
return prop;
}
The Exception:
java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Properties.java:434)[:1.8.0_91]
at java.util.Properties.load0(Properties.java:353)[:1.8.0_91]
at java.util.Properties.load(Properties.java:341)[:1.8.0_91]
at com.mycompany.util.PropertyLoader.getProperties(PropertyLoader.java:19)[319:camel-archetype-blueprint:0.0.14]
at com.mycompany.camel.blueprint.MyProcessor.process(MyProcessor.java:21)[319:camel-archetype-blueprint:0.0.14]
at org.apache.camel.processor.DelegateSyncProcessor.process(DelegateSyncProcessor.java:63)[231:org.apache.camel.camel-core:2.17.0.redhat-630135]
at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:77)[231:org.apache.camel.camel-core:2.17.0.redhat-630135]
at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:468)[231:org.apache.camel.camel-core:2.17.0.redhat-630135]
at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:196)[231:org.apache.camel.camel-core:2.17.0.redhat-630135]
at org.apache.camel.processor.Pipeline.process(Pipeline.java:121)[231:org.apache.camel.camel-core:2.17.0.redhat-630135]
at org.apache.camel.processor.Pipeline.process(Pipeline.java:83)[231:org.apache.camel.camel-core:2.17.0.redhat-630135]
at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:196)[231:org.apache.camel.camel-core:2.17.0.redhat-630135]
at org.apache.camel.component.timer.TimerConsumer.sendTimerExchange(TimerConsumer.java:192)[231:org.apache.camel.camel-core:2.17.0.redhat-630135]
at org.apache.camel.component.timer.TimerConsumer$1.run(TimerConsumer.java:76)[231:org.apache.camel.camel-core:2.17.0.redhat-630135]
at java.util.TimerThread.mainLoop(Timer.java:555)[:1.8.0_91]
at java.util.TimerThread.run(Timer.java:505)[:1.8.0_91]
Any help would be highly appreciated.
Do not do that. You are looking for trouble.
Load properties the OSGi way (use .cfg as extension and a blueprint property-placeholder bean)
You have the added benefit of getting notified if the file changes (if you wish)
Inject them in a bean EVEN IF you are using only static methods.
Don't mix managed beans with unmanaged static code unless you know very well what you are doing.
If some "static" code requires properties means that it is stateful, and this class deserves to be instantiated to a bean.
Not sure why you are getting an NPE without a more complete example. If you need to use properties without a route, you should be using Camel's property placeholder facilities:
https://access.redhat.com/documentation/en-us/red_hat_jboss_fuse/6.3/html/apache_camel_development_guide/basicprinciples#BasicPrinciples-PropPlaceholders
i am trying to use JournalArticleServiceUtil class to get web content, but it shows an error and i don't know how to fix it, there's my code
long groupId = themeDisplay.getLayout().getGroupId();
System.out.println("GroupId: " + groupId);
List<JournalArticle> articleList = new ArrayList<>();
List<String> news = new ArrayList<>();
try {
DDMStructure structure = DDMStructureManagerUtil.getStructure(Long.parseLong("94203"));
articleList = JournalArticleServiceUtil.getArticlesByStructureId(groupId, structure.getStructureKey(), 0, 10, null);
for (JournalArticle art: articleList) {
news.add(art.getContent());
}
} catch (SystemException e) {
log.error(e.getMessage());
} catch (NumberFormatException e) {
log.error(e.getMessage());
} catch (PortalException e) {
log.error(e.getMessage());
}
the error says
com.sun.proxy.$Proxy466 cannot be cast to com.liferay.journal.service.JournalArticleService
i hope you can help me with this
This seems related to the implementation version of the service class you are using. If you are using gradle, you could give preference to compileOnly rules and make sure you match the kernel version in you server with the one you are using. Some times you do need to declare implementation versions directly on you bnd.bnd.
Something like:
Import-Package: com.liferay.journal.model;version="[1.0.0,3.0.0)", com.liferay.journal.service; version="[1.0.0,3.0.0)",*
I think you should try using JournalArticleLocalServiceUtil instead of JournalArticleServiceUtil.
I am using Lombok's #Cleanup and even when I find it very useful, I cannot figure out how can I catch an exception being thrown by the main block of code that makes use of the resource to be cleant.
My code is the following:
ScriptRunner runner = null;
try {
runner = scriptRunner();
for (Resource sqlFile : sqlScripts) {
runScript(sqlFile.getFile(), runner);
}
} catch (Exception ex) {
log.error("Error found when attempting to run script", ex);
} finally {
if (runner != null) {
runner.closeConnection();
}
}
With Lombok, I could simplify the code above this way:
#Cleanup("closeConnection") ScriptRunner runner = scriptRunner();
for (Resource sqlFile : sqlScripts) {
runScript(sqlFile.getFile(), runner);
}
But I would be missing the possibility of catching any exception that might arise when the for loop executes. Is there any way to do this in an elegant way with Lombok? (i.e. other than programmatically using "try /catch?)
Thanks very much in advance.
No. If you want to process the exception you need to catch it, and for that you'll need a try-catch block.
Full Disclosure: I am one of the Project Lombok developers.