I'm new to soft. engineering and doing a project for user management and inside of Security package, I have a class called SecurityAspects where I define #Pointcut and #Around.
I'm using Apache Ant to compile the whole program.
SecurityAspects.java
package teste.servicepack.security.logic;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import teste.domain.UserSession;
import teste.servicepack.security.SecurityContextProvider;
import teste.servicepack.security.logic.Exception.FailRoleException;
import teste.servicepack.security.logic.Exception.NotAuthenticatedException;
import teste.servicepack.security.logic.Permission.HasRole;
import teste.utils.HibernateUtils;
import java.util.Arrays;
import java.util.logging.Logger;
#Aspect
public class SecurityAspects {
private static final Logger logger = Logger.getLogger(String.valueOf(SecurityAspects.class));
#Pointcut("#annotation(Transaction)")
public void TransactionPointCut(){}
#Pointcut("#annotation(IsAuthenticated)")
public void isAuthenticatedPointCut(){}
#Pointcut("#annotation(hasRole)")
public void hasRolePointCut(HasRole hasRole){}
#Pointcut("execution(* *(..))")
public void executionPointCut(){}
//Transaction
#Around("TransactionPointCut() && executionPointCut()")
public Object transactionAdvise(ProceedingJoinPoint pjp) throws Throwable{
HibernateUtils.getCurrentSession().beginTransaction();
try {
Object obj = pjp.proceed();
HibernateUtils.getCurrentSession().getTransaction().commit();
logger.info("Transaction finished successfully!");
return obj;
}catch (Exception e){
HibernateUtils.getCurrentSession().getTransaction().rollback();
throw e;
}
}
// isAuthenticated
#Around("isAuthenticatedPointCut() && executionPointCut()")
public Object isAuthenticatedAdvise(ProceedingJoinPoint pjp) throws Throwable
{
logger.info("isAuthenticated");
String cookie = SecurityContextProvider.getInstance().getSecuritySessionContext().getRequester();
UserSession session = (UserSession) HibernateUtils.getCurrentSession().load(UserSession.class,cookie);
if(session.getUser() != null)
return pjp.proceed();
throw new NotAuthenticatedException("Access Denied, not authenticated at " + pjp.getSourceLocation().getFileName() + " " + pjp.getSourceLocation().getLine() + " service: " + pjp.getSignature().getName());
}
// HasRole
#Around("hasRolePointCut(hasRole) && executionPointCut()")
public Object hasRoleAdvise(ProceedingJoinPoint pjp,HasRole hasRole) throws Throwable
{
logger.info("hasRole");
String cookie = SecurityContextProvider.getInstance().getSecuritySessionContext().getRequester();
UserSession session = (UserSession) HibernateUtils.getCurrentSession().load(UserSession.class,cookie);
String[] rolesIn = hasRole.role().split(",");
String[] roles = session.getUser().getRoles().split(",");
for(String checkRole: rolesIn){
if(Arrays.asList(roles).contains(checkRole)) {
return pjp.proceed();
}
}
throw new FailRoleException("Access Denied, does not have role " + hasRole.role() + " at " + pjp.getSourceLocation().getFileName() + " " + pjp.getSourceLocation().getLine() + " service: " + pjp.getSignature().getName());
}
}
build.xml
<?xml version="1.0"?>
<project default="deploy" basedir=".">
<property file="local.properties"/>
<property file="build.properties"/>
<path id="pathref">
<fileset dir="lib/hibernate">
<include name="*.jar"/>
</fileset>
<fileset dir="lib/mysql">
<include name="*.jar"/>
</fileset>
<fileset dir="lib/commons">
<include name="*.jar"/>
</fileset>
<fileset dir="lib/log4j">
<include name="*.jar"/>
</fileset>
<fileset dir="lib/json">
<include name="*.jar"/>
</fileset>
<fileset dir="${TOMCAT_HOME}/lib">
<include name="servlet-api.jar"/>
</fileset>
</path>
<taskdef name="hibernatetool"
classname="org.hibernate.tool.ant.HibernateToolTask"
classpathref="pathref" classpath="${build.dir.classes}"/>
<taskdef name="schemaupdate"
classname="org.hibernate.tool.hbm2ddl.SchemaUpdateTask"
classpathref="pathref" classpath="${build.dir.classes}"/>
<target name="generateUpdateHibernateSql" depends="compile">
<schemaupdate
properties="${build.dir.classes}/teste/domain/jdbc.properties"
quiet="no"
text="no">
<fileset dir="src/java">
<include name="**/*.hbm.xml"/>
</fileset>
</schemaupdate>
</target>
<target name="generateHibernateDomainObjects">
<mkdir dir="src/gen"/>
<replace dir="src/java" value="">
<include name="**/*.hbm.xml"/>
<replacefilter token='<timestamp source="db"' value="<timestamp"/>
</replace>
<hibernatetool>
<configuration>
<fileset dir="src/java">
<include name="**/*.hbm.xml"/>
</fileset>
</configuration>
<hbm2java
jdk5="true"
ejb3="false"
destdir="src/gen"/>
</hibernatetool>
<delete>
<fileset dir="src/gen">
<include name="**/*Impl.java"/>
</fileset>
</delete>
<replace dir="src/java" value="">
<include name="**/*.hbm.xml"/>
<replacefilter token='<timestamp' value='<timestamp source="db"'/>
</replace>
</target>
<target name="initDirs">
<mkdir dir="build"/>
<mkdir dir="build/ant"/>
<mkdir dir="build/ant/classes"/>
<mkdir dir="build/ant/war"/>
<mkdir dir="build/ant/war/WEB-INF"/>
<mkdir dir="build/ant/war/WEB-INF/classes"/>
<mkdir dir="build/ant/war/WEB-INF/lib"/>
</target>
<target name="deploy" depends="build.war">
<copy todir="${TOMCAT_HOME}/webapps/">
<fileset dir="build/ant/">
<include name="war/**/*.*"/>
</fileset>
</copy>
<touch file="${TOMCAT_HOME}/webapps/war/WEB-INF/web.xml"/>
</target>
<target name="build.war" depends="compile">
<copy todir="build/ant/war/WEB-INF" file="conf/web.xml"/>
<copy todir="build/ant/war">
<fileset dir="src/web">
<include name="**/*.*"/>
</fileset>
</copy>
<copy todir="build/ant/war/WEB-INF/classes">
<fileset dir="build/ant/classes">
<include name="**/*.*"/>
</fileset>
</copy>
<copy todir="build/ant/war/WEB-INF/lib">
<fileset dir="lib/hibernate">
<include name="*.jar"/>
</fileset>
<fileset dir="lib/mysql">
<include name="*.jar"/>
</fileset>
<fileset dir="lib/log4j">
<include name="*.jar"/>
</fileset>
<fileset dir="lib/commons">
<include name="*.jar"/>
</fileset>
<fileset dir="lib/json">
<include name="*.jar"/>
</fileset>
</copy>
<touch file="build/ant/war/WEB-INF/web.xml"/>
</target>
<target name="war" depends="build.war">
<war file="build/ant/war.war">
<fileset dir="build/ant/war">
<include name="**/*.*"/>
</fileset>
</war>
</target>
<target name="compile" depends="initDirs">
<javac destdir="build/ant/classes"
debug="true"
encoding="UTF-8"
source="1.8" target="1.8"
classpathref="pathref">
<src path="src/java"/>
<src path="src/gen"/>
</javac>
<copy file="conf/log4j.properties" todir="build/ant/classes"/>
<copy file="conf/hibernate.cfg.xml" todir="build/ant/classes/teste/domain"/>
<copy todir="build/ant/classes">
<fileset dir="src/java">
<include name="**/*.xml"/>
</fileset>
</copy>
<copy file="conf/jdbc.properties" todir="${build.dir.classes}/teste/domain"/>
<replace file="${build.dir.classes}/teste/domain/jdbc.properties">
<replacefilter token="#database.username#" value="${database.username}"/>
<replacefilter token="#database.password#" value="${database.password}"/>
<replacefilter token="#database.connection.url#" value="${database.connection.url}"/>
</replace>
<replace file="${build.dir.classes}/teste/domain/hibernate.cfg.xml">
<replacefilter token="#database.username#" value="${database.username}"/>
<replacefilter token="#database.password#" value="${database.password}"/>
<replacefilter token="#database.connection.url#" value="${database.connection.url}"/>
<replacefilter token="#hibernate.show.sql#" value="${hibernate.show.sql}"/>
</replace>
</target>
</project>
But my problem is, when I try to compile with Ant, I have this error:
/Users/dilantaskin/Downloads/TrabalhoES/src/java/teste/servicepack/security/logic/SecurityAspects.java:3: error: package org.aspectj.lang does not exist
/Users/dilantaskin/Downloads/TrabalhoES/src/java/teste/servicepack/security/logic/SecurityAspects.java:4: error: package org.aspectj.lang.annotation does not exist
Like that I have over 20 errors just because of org.aspectj.lang.
In ide, everything looks fine, nothing stays red like it doesn't support.
I can see that I have aspectjrt.jar and aspectj-1.9.7 inside of External Libraries.
I tried to change the org.aspectj.lang to org.aspectj.lang3 but it doesn't recognize.
Can anyone explain me why do I get errors even though I have necessary jars?
INFO: Im not using Maven and I think(?) neither Spring. I have a basic Java project on IntelliJ using Tomcat, Hibernate, Servlet and Ant.
Edit by kriegaex: The author shared an MCVE on GitHub under Edifie/user-management-NF, making the problem reproducible (if first you also download and unzip Tomcat, then point the Ant build to it).
I inspected your project on GitHub, thanks for the link. There is so much wrong with it, I hardly know where to begin:
You committed library JARs instead of doing proper dependency management using Maven, Gradle or, if you insist to stay on Ant, something like Ivy. Dependencies should be downloaded during the build, not committed into a source code management (SCM) repository. Using Maven or Gradle also would have the advantage that IDEs like IntelliJ IDEA or Eclipse can automatically import your projects and also know where to find the dependencies and share them between projects, instead of you redundantly committing them to each of your project SCM repositories and manually adding them to your IDE project configuration. That is just ugly.
One of the JARs in your project is aspectj-1.9.6.jar. This is not what you want but an executable installer, the purpose of which is to install AspectJ locally. In there, you also find nested JARs such as aspectjrt.jar (runtime), aspectjtools.jar (compiler), aspectjweaver.jar (load-time weaving agent). You would have to execute that installer and then copy the libraries you need to your lib directory.
In order for the Java compiler to recognise your AspectJ imports, you need the AspectJ runtime on your classpath, i.e. you can download aspectjrt-1.9.6.jar from Maven Central (select "Downloads → jar" in the top right menu). Then you simply copy it to lib and add this to your Ant script:
<fileset dir="lib">
<include name="aspectjrt*.jar"/>
</fileset>
Now the project compiles, but if that achieves what you want depends on whether you want to use Spring AOP or native AspectJ. For the former, no AspectJ compiler is necessary, but your aspects would have to be made Spring components. For the latter, compiling with javac is not enough, you need the AspectJ compiler ajc. AspectJ provides its own Ant task for that. If you need the Ant task, you also want to have the AspectJ compiler on that task's classpath (not on the application classpath, only during build time). That would be aspectjtools-1.9.6.jar.
I am not an Ant user, so I have zero interest in this ancient build tool, but if you are an Ant user, you will know how to finish the job.
There is more wrong with your application, e.g. like I said in my comment, #annotation(Transaction) and #annotation(IsAuthenticated) should use fully qualified class names or import the classes and bind the annotations to pointcut parameters, if that is what you need.
I think, you should take a break and learn some Ant and AspectJ basics first, probably also Git basics. Or if you have a choice to switch from Ant to Maven, I strongly advise you to do that and forget Ant. If you are stuck with Ant because your employer requires you to use it, my sincere condolences. But actually, it is just a tool, and if you need it, just learn how to handle it. 🙂
P.S.: The current version of AspectJ is 1.9.8. If you like, you can upgrade.
When attempting to run tests with JUnit 5 via the Ant junitlauncher task, I receive the following message:
[junitlauncher] WARNING: TestEngine with ID 'junit-jupiter' failed to execute tests
[junitlauncher] java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing
How do I fix this so that my tests can run?
The answers to similar questions (e.g. this one) mainly say to make sure that hamcrest is on the classpath. I've verified using the print-test-classpath target (see build file below for details) that the classpath I'm using to run my tests includes hamcrest-2.1.jar. Furthermore, the tests compile without any issue.
Here are the sections of my build.xml I think are relevant:
<path id="lib-only-class.path">
<fileset dir="${lib.dir}">
<include name="**/*.jar" />
</fileset>
</path>
<path id="class.path">
<path refid="lib-only-class.path"/>
<pathelement location="${classes.dir}"/>
</path>
<path id="test-class.path">
<path refid="class.path"/>
<pathelement location="${test-classes.dir}"/>
</path>
<target name="compile-tests" depends="compile">
<mkdir dir="${test-classes.dir}"/>
<javac srcdir="${test.dir}" destdir="${test-classes.dir}"
includeantruntime="no" classpathref="class.path"/>
</target>
<target name="test" depends="compile-tests">
<junitlauncher printsummary="yes">
<classpath refid="test-class.path"/>
<testclasses>
<fileset dir="${test-classes.dir}"/>
</testclasses>
</junitlauncher>
</target>
<target name="print-test-classpath">
<pathconvert property="classpathInName" refid="test-class.path"/>
<echo>Classpath is ${classpathInName}</echo>
</target>
I am trying to compile an adroid project using ant which contains multiple sources and external 3rd party jars.
It works fine with eclipse but i require it to run through command line using ant.
my custom_rules.xml looks like this:
<?xml version="1.0" encoding="UTF-8"?> <project name="CustomRules">
<target name="-pre-build" >
<copy todir="tmp-src" >
<fileset dir="src" includes="**" />
<fileset dir="talkAndroid" includes="**" />
<fileset dir="commonAndroid" includes="**" />
<fileset dir="cocos2dAndroid" includes="**" />
</copy>
<classpath id="classpath" description="The default classpath.">
<pathelement path="${classpath}"/>
<fileset dir="external_libs">
<include name="*.jar"/>
</fileset>
</classpath>
</target>
<target name="-post-build" >
<delete dir="tmp-src" />
</target> </project>
it isn't working for external_libs and unable to find external libraries, if I copy these jars from external_libs to libs folder ... duplicate class found errors are generated!
What should I do?
I am trying to convert a web service from using the version of ant that comes with weblogic (OFM 11G Release 1 (10.3.6)) to a newer, standalone version of ant (1.8.4). During the build with 1.8.4 I am receiving the following warnings which I would like to eliminated if possible. The warning occurs with all exception classes in the project. Can anyone provide a suggestion? I am new to weblogic web services.
[jwsc] [WARNING] skipping exception constructor for ca.bc.gov.webade.WebADEException: could not find property corresponding to ctor param 'arg0' on public WebADEException(java.lang.String,java.lang.Exception)
Here is the JWSC portion of the build file. The task is defined using the same paths as part of the build classpath below. The weblogic paths correspond to to WLS_HOME and MODULES_DIR environment variables as set by weblogic.
<target name="jwsc" depends="compile">
<path id="JWSCclasspath">
<fileset dir="${lib.dir}">
<include name="*.jar"/>
</fileset>
<fileset dir="${webade.lib.dir}" includes="*.jar" />
<!-- Reference compiled classes rather than jar file due to issue deleting the jar file at the end of the build -->
<pathelement path="${build.bus.dir}"/>
<pathelement path="{weblogic.dir}"/>
<fileset dir="${weblogic.dir}" >
<include name="*.jar" />
</fileset>
<pathelement path="${weblogic.modules}" />
<fileset dir="${weblogic.modules}" >
<include name="*.jar"/>
</fileset>
</path>
<jwsc srcdir="${src.dir}" destdir="${deploy.dir}" classpathref="JWSCclasspath">
<jws file="${war.path}/MapViewerServicesPortTypeImpl.java" type="JAXRPC">
<WLHttpTransport contextPath="mapViewer" serviceUri="MapViewerWS" portName="MapViewerServicesPort"></WLHttpTransport>
</jws>
</jwsc>
</target>
I am getting the following exception when running an applet:
Exception in thread "AWT-EventQueue-4" java.lang.NoClassDefFoundError: ice/net/SnapshotCacheManager
but the file is inside the jar.
I searched online and found it might be related to the applet not looking in the current directory and i need to add .; to the CLASSPATH but i am not sure how to add it to the build.xml
Thanks
Doron
Edit: Finally I figured it out, it wasn't an ant problem or the build XML, I got this exception because I signed two jars containing the same package differently, so there was a collision, not a very informative exception....
it might be useful to see what is in your current build.xml file, but the section you probably want to look at is the <target> element specifically the <src path> and <fileset> elements. Here is a VERY rough example with some guiding variables.
<property name="classes.home" value="/myproject/src"/>
<target name="compile_myproject" depends="clean">
<javac destdir="${classes.home}" debug="off" optimize="on" deprecation="on">
<classpath>
<fileset dir="/location/of/jars/">
<include name="*.jar"/>
<exclude name="jar-I-dont-want.jar"/>
</fileset>
<fileset dir="/location/of/axis2/jars">
<include name="**/*.jar"/>
</fileset>
</classpath>
<src path="${classes.home}"/>
<include name="/test/**/*.java"/>
<include name="other/location/*.java"/>
<exclude name="/debug/and/useless/files/**/*.java"/>
</javac>
</target>
note that ${classes.home} is a special variable defined at the top of the build.xml file. Many variables can be used to make things easier and specify relative paths.