added exception handling mistakes of project
This commit is contained in:
parent
14e4ad948c
commit
6c7fd837ad
|
@ -86,6 +86,24 @@
|
|||
\end{itemize}
|
||||
\end{itemize}
|
||||
|
||||
|
||||
\subsection{Annotationen - Details}
|
||||
\begin{minted}[linenos,breaklines=true]{java}
|
||||
|
||||
CascadeType anschauen
|
||||
|
||||
@Entity
|
||||
@Table(name = "user")
|
||||
public class User extends AbstractEntity {
|
||||
private static final long serialVersionUID = -7060150053795176748L;
|
||||
|
||||
@OneToMany(
|
||||
mappedBy = "communityAdminUser",
|
||||
cascade = {CascadeType.PERSIST, CascadeType.REFRESH}, // Persist Operation des User werden auch auf dessen communityAdminUser persistiert - und auch update
|
||||
orphanRemoval = true //
|
||||
)
|
||||
\end{minted}
|
||||
|
||||
\section{Konfigurationsdateien}
|
||||
\subsection{standalone-psoe.xml}
|
||||
|
||||
|
@ -96,18 +114,18 @@ JBoss EAP benutzt standardmäßig die standalone.xml Konfigurationsdatei, kann a
|
|||
|
||||
Abschnitte der standalone.xml
|
||||
\begin{itemize}
|
||||
\item extensions (z.B. diverse Wildfly Module)
|
||||
\item management (z.B. Access Control -> role-mapping)
|
||||
\item profile (z.B. JPA Subsystem)
|
||||
\item interfaces (z.B. \${jboss.bind.address:127.0.0.1})
|
||||
\item socket-binding-group (z.B \${jboss.http.port:8080})
|
||||
\item extensions (z.B. diverse Wildfly Module)
|
||||
\item management (z.B. Access Control -> role-mapping)
|
||||
\item profile (z.B. JPA Subsystem)
|
||||
\item interfaces (z.B. \${jboss.bind.address:127.0.0.1})
|
||||
\item socket-binding-group (z.B \${jboss.http.port:8080})
|
||||
\end{itemize}
|
||||
|
||||
\begin{code}
|
||||
\captionof{listing}{standalone.xml (auszugsweise)}
|
||||
\captionof{listing}{standalone.xml (auszugsweise)}
|
||||
|
||||
|
||||
\begin{minted}[linenos,breaklines=true]{xml}
|
||||
\begin{minted}[linenos,breaklines=true]{xml}
|
||||
|
||||
........
|
||||
|
||||
|
@ -143,23 +161,23 @@ Abschnitte der standalone.xml
|
|||
|
||||
\subsection{persistence.xml}
|
||||
|
||||
Die Datei \emph{persistence.xml} ist der zentrale Bestandteil der Persistierungs-Konfiguration.
|
||||
Die Datei \emph{persistence.xml} ist der zentrale Bestandteil der Persistierungs-Konfiguration.
|
||||
|
||||
Folgende Dinge können konfiguriert werden:
|
||||
|
||||
\begin{itemize}
|
||||
\item SQL dialect
|
||||
\item the persistence provider that shall be used at runtime
|
||||
\item the data source you want to use to connect to your database
|
||||
\item several provider-specific configuration parameters
|
||||
\item SQL dialect
|
||||
\item the persistence provider that shall be used at runtime
|
||||
\item the data source you want to use to connect to your database
|
||||
\item several provider-specific configuration parameters
|
||||
\end{itemize}
|
||||
|
||||
|
||||
\begin{code}
|
||||
\captionof{listing}{persistence.xml}
|
||||
|
||||
|
||||
\begin{minted}[linenos,breaklines=true]{xml}
|
||||
\captionof{listing}{persistence.xml}
|
||||
|
||||
|
||||
\begin{minted}[linenos,breaklines=true]{xml}
|
||||
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<persistence version="2.0"
|
||||
|
@ -197,8 +215,8 @@ SQL stdout logging
|
|||
\end{figure}
|
||||
\subsection{web.xml}
|
||||
\begin{itemize}
|
||||
|
||||
%TODO des is zu ungenau!
|
||||
|
||||
%TODO des is zu ungenau!
|
||||
\item konfiguriert den Java Webserver (Wildfly - JBOSS)
|
||||
\item befindet sich im Ordner \textbf{src/main/webapp/WEB-INF/web.xml}
|
||||
\end{itemize}
|
||||
|
@ -307,17 +325,17 @@ SQL stdout logging
|
|||
\begin{itemize}
|
||||
\item Resource plugin – klar für Ressourcen
|
||||
\item Wildfly – server
|
||||
\begin{multicols}{2}
|
||||
\begin{enumerate}
|
||||
\item Compile
|
||||
\item Surefire (unitTests)
|
||||
\item Packaging - war file erstellen
|
||||
\item Wildfly - fressen und deployen
|
||||
\item Failsafe IT-test
|
||||
\item MVN site
|
||||
\item Gui test
|
||||
\end{enumerate}
|
||||
\end{multicols}
|
||||
\begin{multicols}{2}
|
||||
\begin{enumerate}
|
||||
\item Compile
|
||||
\item Surefire (unitTests)
|
||||
\item Packaging - war file erstellen
|
||||
\item Wildfly - fressen und deployen
|
||||
\item Failsafe IT-test
|
||||
\item MVN site
|
||||
\item Gui test
|
||||
\end{enumerate}
|
||||
\end{multicols}
|
||||
\item Primeafce = jsf Framework
|
||||
\item Jacoco = test Coverage
|
||||
\item Slf4j = logger
|
||||
|
@ -334,4 +352,162 @@ DAO wirft Exception – im ServiceLayer wird dies gefangen und der Stack-Trace w
|
|||
Alle Patterns, die vorkommen – praktische Beispiele aus dem Code
|
||||
|
||||
Was sind JavaBeans? Wie funktioniert das Konzept? Wie wird es genau implementiert?
|
||||
NamedBean, TypedBean etc.
|
||||
NamedBean, TypedBean etc.
|
||||
|
||||
\section{Fehler im Projekt}
|
||||
\subsection{Return null}
|
||||
Anstatt von Null einfach eine Leere Liste bzw. ein default Objekt (oder new <Object>) zurückgeben
|
||||
\subsection{Exception nicht gefangen}
|
||||
\begin{minted}[linenos,breaklines=true]{java}
|
||||
package at.fhj.swd.psoe.service.impl;
|
||||
...
|
||||
@Override
|
||||
public void removeCommunityByAdmin(CommunityDTO communityDTO) {
|
||||
Community community = communityDAO.findById(communityDTO.getId());
|
||||
String errorText;
|
||||
try {
|
||||
communityDAO.removeCommunityByAdmin(community);
|
||||
} catch (DaoException e) {
|
||||
errorText = "Error removing community";
|
||||
logger.error(errorText, e);
|
||||
throw new ServiceException(errorText);
|
||||
}
|
||||
}
|
||||
\end{minted}
|
||||
\subsection{Destructive Wrapping}
|
||||
\begin{minted}[linenos,breaklines=true]{java}
|
||||
package at.fhj.swd.psoe.service.impl;
|
||||
...
|
||||
@Override
|
||||
public CommunityDTO updateCommunityEnabled(String adminUserId, String communityName, boolean isEnabled) {
|
||||
String errorText = "";
|
||||
try {
|
||||
boolean hasPermission = false;
|
||||
User adminUser = userDao.getByUserId(adminUserId);
|
||||
Community community = communityDAO.getByName(communityName);
|
||||
|
||||
|
||||
for (Role r : adminUser.getRoles()) {
|
||||
if (r.getname().equals("ADMIN") || r.getname().equals("PORTALADMIN")) {
|
||||
hasPermission = true;
|
||||
}
|
||||
}
|
||||
if (hasPermission || adminUser.getUserId().equals(community.getCommunityAdminUser().getUserId())) {
|
||||
community.setIsEnabled(isEnabled);
|
||||
communityDAO.update(community);
|
||||
} else {
|
||||
errorText = "No Permission to update community";
|
||||
throw new AuthenticationException(errorText);
|
||||
}
|
||||
return CommunityMapper.toDTO(community);
|
||||
} catch (DaoException e) {
|
||||
errorText = "Error updating community enabled";
|
||||
throw new ServiceException(errorText);
|
||||
} catch (AuthenticationException e) {
|
||||
throw new ServiceException(errorText);
|
||||
} catch (Throwable e) {
|
||||
errorText = "Unknown error updating community enabled";
|
||||
throw new ServiceException(errorText);
|
||||
}
|
||||
}
|
||||
\end{minted}
|
||||
\subsection{Logger mit falschem Parameter}
|
||||
\begin{minted}[linenos,breaklines=true]{java}
|
||||
package at.fhj.swd.psoe.service.impl;
|
||||
...
|
||||
@Stateless
|
||||
public class DepartmentHierarchyServiceImpl implements DepartmentHierarchyService, Serializable {
|
||||
|
||||
private static final long serialVersionUID = -2467949382018996094L;
|
||||
private static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);
|
||||
...
|
||||
\end{minted}
|
||||
\subsection{Fehlendes Exception Handling}
|
||||
\begin{minted}[linenos,breaklines=true]{java}
|
||||
package at.fhj.swd.psoe.service.impl;
|
||||
...
|
||||
@Local(MessageService.class)
|
||||
@Remote(MessageServiceRemote.class)
|
||||
@Stateless
|
||||
public class MessageServiceImpl implements MessageService, MessageServiceRemote, Serializable {
|
||||
...
|
||||
@Override
|
||||
public MessageDTO getByMessageId(long id) {
|
||||
Message message = messageDAO.getById(id);
|
||||
if (message == null) {
|
||||
return null;
|
||||
}
|
||||
return MessageMapper.toDTO(message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MessageDTO updateByAdmin(long id, String content, Date changed) {
|
||||
Message message = messageDAO.getById(id);
|
||||
message.setContent(content);
|
||||
message.setEditedByAdmin(changed);
|
||||
return MessageMapper.toDTO(messageDAO.update(message));
|
||||
}
|
||||
|
||||
@Override
|
||||
public MessageDTO updateByUser(long messageId, String content, Date changed) {
|
||||
Message message = messageDAO.getById(messageId);
|
||||
message.setContent(content);
|
||||
message.setEditedByUser(changed);
|
||||
return MessageMapper.toDTO(messageDAO.update(message));
|
||||
}
|
||||
\end{minted}
|
||||
\subsection{Exception werfen und gleich wieder fangen}
|
||||
\begin{minted}[linenos,breaklines=true]{java}
|
||||
...
|
||||
public class MessageServiceImpl implements MessageService, MessageServiceRemote, Serializable {
|
||||
private static final long serialVersionUID = 6768291437557855130L;
|
||||
...
|
||||
// nicht optimal, da die IllegalArgumentException gleich wieder gefangen wird
|
||||
// überdies wird alles andere nicht gefangen
|
||||
|
||||
|
||||
@Override
|
||||
public void deleteMessage(long id) {
|
||||
try {
|
||||
Message message = messageDAO.getById(id);
|
||||
if (message == null) {
|
||||
throw new IllegalArgumentException("Message cannot be empty");
|
||||
}
|
||||
messageDAO.delete(message);
|
||||
logger.info("Message deleted successfully");
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
String errorMsg = "Could not delete the message (illegal argument)";
|
||||
logger.error(errorMsg, ex);
|
||||
throw new ServiceException(errorMsg);
|
||||
}
|
||||
}
|
||||
|
||||
//---------- besser wäre
|
||||
package at.fhj.swd.psoe.service.impl;
|
||||
...
|
||||
// erst loggen, dass man in die Methode gekommen ist
|
||||
// wenn userDTO null ist wird IllegalArgument geworfen und das außerhalb des try catch blocks
|
||||
// erst wird die DaoException gefangen und anschließend alle anderen
|
||||
// Stacktrace wird geloggt und jeweils die ServiceException weitergegeben
|
||||
@Override
|
||||
public void saveUser(UserDTO userDTO) {
|
||||
logger.debug("UserService saveUser() called with parameter: '{}'", userDTO);
|
||||
|
||||
if (userDTO == null) {
|
||||
throw new IllegalArgumentException("userDTO is null");
|
||||
}
|
||||
|
||||
try {
|
||||
User user = (userDTO.getId() == null) ? new User() : userDao.getById(userDTO.getId());
|
||||
userDao.update(UserMapper.toEntity(userDTO, user));
|
||||
} catch (DaoException e) {
|
||||
logger.error("Error saving user", e);
|
||||
throw new ServiceException("Error saving user");
|
||||
} catch (Throwable e) {
|
||||
logger.error("Unknown error saving user", e);
|
||||
throw new ServiceException("Unknown error saving user");
|
||||
}
|
||||
}
|
||||
\end{minted}
|
||||
|
||||
|
|
Loading…
Reference in New Issue